首页>代码>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; } }

lcqlcl LV11
2023年8月29日
lironggang LV38
2023年3月31日
annazhang LV29
2022年12月1日
zw050256 LV7
2022年9月30日
wuyu8995861 LV7
2022年7月26日
zxysss LV3
2022年1月6日
cwx1234 LV11
2021年10月8日
123456nty LV37
2021年9月18日
newhaijun LV16
2021年8月23日
zy_286315 LV7
2021年8月4日

xianyu091012 LV5
2024年12月27日
WBelong LV8
2023年12月19日
lichengai LV7
2023年11月27日
漫步的海星 LV4
2023年9月21日
lcqlcl LV11
2023年8月29日
cwb6357123 LV6
2023年6月30日
CL200228 LV4
2023年4月29日
mzj695
2023年4月6日
暂无贡献等级
lironggang LV38
2023年3月31日
清横白川玉 LV6
2023年1月3日