首页>代码>springBoot+mybatis+vue+html5+apacheFTP+element-ui+layui实现页面多附件上传FTP服务器、下载及文件预览>/TestProject/src/main/java/com/test/service/FileService.java
package com.test.service; import com.alibaba.fastjson.JSONObject; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.UUID; @Service public class FileService { @Resource FTPService ftpService; public JSONObject uploadFileToFTP (HttpServletRequest request, HttpServletResponse response, JSONObject paramsJO) { JSONObject jo = new JSONObject(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String nowDateStr = format.format(new Date()); String storePath = "/" + request.getContextPath() + nowDateStr.substring(0,4) + "/" + nowDateStr.substring(4,6) + "/" + nowDateStr.substring(6,8); //要检查是不是enctype = multipart/form-data提交过来的数据 boolean isMultipart = ServletFileUpload.isMultipartContent(request); //如果不是,需要处理 if (!isMultipart) { jo.put("message", "请将 enctype 设置成 multipart/form-data 类型"); } else { //去生成一个文件上传的FileUpload对象 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if(StringUtils.isBlank(item.getName())){ continue; } if (!item.isFormField()) { jo.put("file_id_", UUID.randomUUID().toString().toUpperCase()); jo.put("file_name_", item.getName()); jo.put("file_format_", item.getName().substring( item.getName().lastIndexOf(".") + 1)); jo.put("file_rename_", jo.getString("file_id_") + "." + item.getName().substring(item.getName().lastIndexOf(".") + 1)); jo.put("file_path_", storePath + "/" + jo.getString("file_rename_")); InputStream in = item.getInputStream(); boolean create_dir_flag_ = ftpService.createFTPDir(storePath); System.out.println("文件目录:" + storePath + " 是否创建成功(true/false):" + create_dir_flag_ ); boolean upload_flag_ = ftpService.upload(jo.getString("file_path_"), in); jo.put("upload_flag_", upload_flag_); System.out.println("文件名:" + item.getName() + " 上传是否成功(true/false):" + upload_flag_ ); } } } catch (Exception e) { e.printStackTrace(); } } return jo; } public JSONObject downloadFileToFTP (HttpServletRequest request, HttpServletResponse response, JSONObject paramsJO) { JSONObject jo = new JSONObject(); FileOutputStream out = null; if (paramsJO!=null && !paramsJO.isEmpty() && StringUtils.isNotBlank(paramsJO.getString("file_path_"))) { String file_path_ = paramsJO.getString("file_path_"); String project_root_path_ = request.getSession().getServletContext().getRealPath("/"); File fileCachePath = new File(project_root_path_ + file_path_.substring(0, file_path_.lastIndexOf("/"))); boolean downloadFlag = false; if (!fileCachePath.exists()) { boolean mksFlag = fileCachePath.mkdirs(); System.out.println("创建目录"+fileCachePath+"===(true/false)==="+mksFlag); try { out = new FileOutputStream(project_root_path_ + file_path_); } catch (FileNotFoundException e) { e.printStackTrace(); } downloadFlag = ftpService.download(file_path_, out); } else if (fileCachePath.exists()) { if (fileCachePath.length()>0) { downloadFlag = true; } else { try { out = new FileOutputStream(project_root_path_ + file_path_); } catch (FileNotFoundException e) { e.printStackTrace(); } downloadFlag = ftpService.download(file_path_, out); } } if (downloadFlag) { jo.put("downloadFlag", downloadFlag); } } return jo; } }