网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java程序图片上传代码 java图片上传工具类

java图片批量上传代码?

用struts也可以实现 多文件上传

专注于为中小企业提供成都做网站、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业青龙免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

下面是我写的代码,作为参考!

/*文件目录*/

public static String [] fileArray={

"logo.png",

"index.swf",

"OEMInfo.txt",

"favicon.ico"};

/**

* @author Caoshun

* @see 接收并保存文件

* */

public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){

String fileName="";

//获取表单中的文件资源

HashtableObject, Object files = form.getMultipartRequestHandler().getFileElements();

//遍历文件,并且循环保存

//当前处理文件序号

int file_num=1;

for (EnumerationObject e = files.keys(); e.hasMoreElements();) {

/*根据处理的当前文件下标,确定文件名*/

fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());

if (file != null file.getFileSize() 0) {

try {

//使用formfile.getInputStream()来获取一个文件的输入流进行保存。

//文件名

//String fileName = file.getFileName();

//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);

//文件大小

//int fileSize = file.getFileSize();

//文件流

InputStream is = file.getInputStream();

//将输入流保存到文件

//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中写入

File rf = new File(rootPath1);

FileOutputStream fos = null;

fos = new FileOutputStream(new File(rf, fileName));

byte[] b = new byte[10240];

int real = 0;

real = is.read(b);

while (real 0) {

fos.write(b, 0, real);

real = is.read(b);

}

//往en中写入

File rf2 = new File(rootPath2);

InputStream is2 = file.getInputStream();

FileOutputStream fos2 = null;

fos2 = new FileOutputStream(new File(rf2, fileName));

byte[] b2 = new byte[10240];

int real2 = 0;

real2 = is2.read(b2);

while (real2 0) {

fos2.write(b2, 0, real2);

real2 = is2.read(b2);

}

//关闭文件流

fos.close();

is.close();

fos2.close();

is2.close();

} catch (RuntimeException e1) {

e1.printStackTrace();

} catch (Exception ee) {

ee.printStackTrace();

}

file.destroy();

}

file_num++;

}

}

java怎样上传图片(写个例子谢谢);

我有一段上传图片的代码,并且可以根据实际,按月或按天等,生成存放图片的文件夹

首先在JSP上放一个FILE的标签这些我都不说了,你也一定明白,我直接把处理过程给你发过去

我把其中存到数据库中的内容删除了,你改一下就能用

/**

*

* 上传图片

* @param servlet

* @param request

* @param response

* @return

* @throws Exception

*/

//这里我是同步上传的,你随意

public synchronized String importPic(HttpServlet servlet, HttpServletRequest request,HttpServletResponse response) throws Exception {

SimpleDate()Format formatDate() = new SimpleDate()Format("yyyyMM");

Date nowtime=new Date();

String formatnowtime=formatDate.format(nowtime);

File root = new File(request.getRealPath("/")+"uploadfile/images/"+formatnowtime+"/"); //应保证在根目录中有此目录的存在 如果没有,下面则上创建新的文件夹

if(!root.isDirectory())

{

System.out.println("创建新文件夹成功"+formatnowtime);

root.mkdir();

}

int returnflag = 0;

SmartUpload mySmartUpload =new SmartUpload();

int file_size_max=1024000;

String ext="";

String url="uploadfile/images/"+formatnowtime+"/";

// 只允许上载此类文件

try{

// 初始化

mySmartUpload.initialize(servlet.getServletConfig(),request,response);

mySmartUpload.setAllowedFilesList("jpg,gif,bmp,jpeg,png,JPG");

// 上载文件

mySmartUpload.upload();

} catch (Exception e){

response.sendRedirect()//返回页面

}

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);

if (myFile.isMissing()){ //没有选择图片做提示!

returnflag = 3;

}else{

String myFileName=myFile.getFileName(); //取得上载的文件的文件名

ext= myFile.getFileExt(); //取得后缀名

if(ext.equals("jpg")||ext.equals("gif")||ext.equals("bmp")||ext.equals("jpeg")||ext.equals("png")||ext.equals("JPG")){ //jpeg,png不能上传!)

int file_size=myFile.getSize(); //取得文件的大小

String saveurl="";

if(file_sizefile_size_max){

try{

//我上面说到,把操作数据库的代友删除了,这里就应该是判断,你的图片是不是已经存在了,存在要怎么处理,不存在要怎么处了,就是你的事了 }

//更改文件名,取得当前上传时间的毫秒数值

Calendar calendar = Calendar.getInstance();

//String filename = String.valueOf(calendar.getTimeInMillis());

String did = contractBean.getMaxSeq("MULTIMEDIA_SEQ");

String filename = did;

String flag = "0";

String path = request.getRealPath("/")+url;

String ename = myFile.getFileExt();

//.toLowerCase()转换大小写

saveurl=request.getRealPath("/")+url;

saveurl+=filename+"."+ext; //保存路径

myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);

//将图片信息插入到数据库中

// ------上传完成,开始生成缩略图-----

java.io.File file = new java.io.File(saveurl); //读入刚才上传的文件

String newurl=request.getRealPath("/")+url+filename+"_min."+ext; //新的缩略图保存地址

Image src = javax.imageio.ImageIO.read(file); //构造Image对象

float tagsize=200;

int old_w=src.getWidth(null);

int old_h=src.getHeight(null);

int new_w=0;

int new_h=0;

int tempsize;

float tempdouble;

if(old_wold_h){

tempdouble=old_w/tagsize;

}else{

tempdouble=old_h/tagsize;

}

// new_w=Math.round(old_w/tempdouble);

// new_h=Math.round(old_h/tempdouble);//计算新图长宽

new_w=150;

new_h=110;//计算新图长宽

BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图

FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);

encoder.encode(tag); //近JPEG编码

newimage.close();

returnflag = 1;

}else{

returnflag = 0;

System.out.println("('上传文件大小不能超过"+(file_size_max/1000)+"K');");

}

}else{

returnflag = 2;

}

}

response.sendRedirect();

return "11";

}

解释一下这段JAVA 关于图片上传的代码

private File file;

private String fileFileName;

private String picture;

//都有getter 和 setter

InputStream is = new FileInputStream(file);

//引入一个IO流的输入流

String root = ServletActionContext.getRequest()

.getRealPath("/bookpicture");

//通过REQUEST来得到相对地址,并在后面加上/bookpicture

File f = new File(root, this.getFileFileName());

//定义一个FILE文件,第一个参数是文件的路径,第二个是文件的名字

picture="."+"\\"+"bookpicture"+"\\"+this.getFileFileName();

//为PICTURE字符串赋值,/地址/文件名

System.out.println

("======picture====="+picture);

//从控制台输出Picture

OutputStream os = new FileOutputStream(f);

//第一个文件的输出流

byte[] buffer = new byte[1024];

//定义一个bufer的字符串,长度为1024

int len = 0;

while ((len = is.read(buffer)) 0) {

//如果从制定文件中读取到的信息为结束就继续循环

os.write(buffer, 0, len);

//将文件读出的内容写入到指定的文件中

}

求JAVA上传图片代码

public String imagesUpload(){

log.debug("images upload");

if(files == null){

saveMessage("没有上传任何文件!");

return ERROR;

}

// 查看上传临时目录是否存在

String sep = File.separator;

String condPath = sep + "upload" + sep +"tmp" +sep;

String uploadDir = ServletActionContext.getServletContext().getRealPath(condPath) + sep;

File dirPath = new File(uploadDir);

if (!dirPath.exists())

dirPath.mkdirs();

String extension, fileName;

int count = 0;

StringBuilder picNameSB = new StringBuilder("");

ListString picNamesSet = new ArrayListString();

try{

for(File tempFile : files){

if(count0)

picNameSB.append("##");

extension = UserUtil.getFileExtension(filesFileName[count]);

fileName = generatePictureName(uploadDir, doType + count, extension);

File newFile = new File(fileName);

UserUtil.copyFileContent(tempFile,newFile);

picNamesSet.add(newFile.getName());

picNameSB.append(newFile.getName());

count++ ;

}

getSession().setAttribute("picNamesSet",picNamesSet);

picName = picNameSB.toString();

}catch(IOException e){

e.printStackTrace();

return ERROR;

}

int x= 1;

return SUCCESS;

}

private String generatePictureName(final String dir, final String suffix, final String fileExtension){

StringBuffer name = new StringBuffer(dir);

String tmpFileName = String.valueOf(System.currentTimeMillis());

if(suffix != null)

tmpFileName += suffix;

tmpFileName += "." + fileExtension;

name.append(tmpFileName);

return name.toString();

}


网页标题:java程序图片上传代码 java图片上传工具类
当前链接:http://bjjierui.cn/article/ddddedp.html

其他资讯