前台多图片上传,Java后台处理后上传到ftp上并回显-java文件传输

本文是《页面ajax上传图片后,后台上传图片服务器后回显》发出后,很多网友说,不能多图片上传的。

所以,本篇介绍多图片上传。

解决思路:

先获取MultipartFile multipartFile 对象。在进行循环遍历得到单个对象。

首先,我们来回顾下,单图片上传的后台代码:

请求参数中:

需要@RequestParam("file")%20MultipartFile%20multipartFile且是必填的。

通过multipartFile.transferTo(file)将图片复制。如下图

这是单图的主要思路。

多图上传开始:

1:创建一个通用的多部分解析器

2:循环得到单个后放到list中:

得到list后,在按照单个图片出来思路进行处理,就可以。

示例代码:

%20@RequestMapping(value%20=%20"/ckeditorUpload")

%20public%20String%20ckeditorUpload(HttpServletResponse%20response

%20,HttpServletRequest%20request

%20)%20throws%20Exception%20{%20

%20InputStream%20is%20=%20null;

OutputStream%20os%20=%20null;

PrintWriter%20out%20=%20null;

String%20path%20=%20request.getSession().getServletContext().getRealPath("/uploadDoc");%20

File%20deleteBigImgFile%20=%20new%20File(path);

try%20{

if(null!=upload){

}

//%20图片名称

%20String%20fileName%20=%20null;

//%20创建一个通用的多部分解析器

%20CommonsMultipartResolver%20multipartResolver%20=%20new%20CommonsMultipartResolver(request.getSession()

%20.getServletContext());

%20//%20MultipartFile%20mfile%20=%20null;

%20List<MultipartFile>%20mfileList%20=%20new%20ArrayList<MultipartFile>();

%20if%20(multipartResolver.isMultipart(request))%20{

//%20转换成多部分request

%20MultipartHttpServletRequest%20multiRequest%20=%20(MultipartHttpServletRequest)%20request;

%20//%20取得request中的所有文件名

%20Iterator<String>%20iter%20=%20multiRequest.getFileNames();

%20while%20(iter.hasNext())%20{

%20MultipartFilemfile%20=%20multiRequest.getFile(iter.next());

%20if%20(mfile%20!=%20null)%20{

%20//%20取得当前上传文件的文件名称

%20String%20myFileName%20=%20mfile.getOriginalFilename();

%20//%20如果名称不为“”,说明该文件存在,否则说明该文件不存在

%20if%20(myFileName.trim()%20!=%20"")%20{

%20//%20获得图片的原始名称

%20String%20originalFilename%20=%20mfile.getOriginalFilename();

%20fileName=originalFilename;

%20}

%20mfileList.add(mfile);

%20}

%20}

%20}

response.setCharacterEncoding("UTF-8");

out%20=%20response.getWriter();

String%20suffix%20=%20fileName.substring(fileName.lastIndexOf("."));

if(!suffix.toLowerCase().equals(".webp")%20&&%20!suffix.toLowerCase().equals(".webp")%20){

out.print("<font%20color=\"red\"size=\"2\">*文件格式不正确(必须为.webp/.webp/.bmp/.webp文件)</font>");

return null;

}

File file = new File(path);

// 如果指定的路径没有就创建

if (!file.exists()) {

file.mkdirs();

}

try {

if(null != mfileList && !mfileList.isEmpty()){

MultipartFile mfileList = mfileList.get(0);

fileName =System.currentTimeMillis()+"_"+UUID.randomUUID().toString()+suffix;

File uploadFile = new File(file +File.separator+fileName);

deleteBigImgFile = new File(file +File.separator+fileName);

mfile.transferTo(uploadFile);

} catch (Exception e) {

System.out.println("上传图片时候异常了:"+e);

e.printStackTrace();

}

}

FileInputStream in1 = null;

String fileName1 = null;

try {

in1 = new FileInputStream(new File(path+"/"+fileName));

fileName1 = System.currentTimeMillis()+"_"+UUID.randomUUID().toString()+suffix;

//防止FTP上传到服务器乱码

String uploadFileName = fileName;

String realName1 = new String(uploadFileName.getBytes("utf-8"), "ISO-8859-1");

fileName1=realName1;

boolean isOk = FTPUpLoad.uploadFile(ftpServer, ftpPort, ftpUser, ftpPassword, fileFtpStortPath, realName1, in1);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}finally{

try {

if(null != deleteBigImgFile && deleteBigImgFile.exists()){

deleteBigImgFile.delete();

}

in1.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if(null!=is){

is.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

推荐阅读