import cn.hutool.core.io.FileUtil; import com.jcraft.jsch.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.SocketException; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Vector;
// -------------------------------本地 文件上传 -------------------------- @RequestMapping(value = "/uploads") public String uploads(@RequestParam("file") MultipartFile file) { try { if (file.isEmpty()) { return "文件为空"; } // 获取文件名 String fileName = file.getOriginalFilename(); log.info("上传的文件名为:" + fileName); // 获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); log.info("文件的后缀名为:" + suffixName); // 设置文件存储路径 String filePath = "F:/Downloads/"; String path = filePath + fileName; File dest = new File(path); // 检测是否存在目录 if (!dest.getParentFile().exists()) { // 新建文件夹 dest.getParentFile().mkdirs(); } // 文件写入 file.transferTo(dest); return "上传成功"; } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "上传失败"; } @PostMapping("/batch") public String handleFileUpload(HttpServletRequest request) { Listfiles = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); String filePath = "F:/"; if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream( new File(filePath + file.getOriginalFilename())));//设置文件路径及名字 stream.write(bytes);// 写入 stream.close(); } catch (Exception e) { stream = null; return "第 " + i + " 个文件上传失败 ==> " + e.getMessage(); } } else { return "第 " + i + " 个文件上传失败因为文件为空"; } } return "上传成功"; } @GetMapping("/downloads") public String downloadFile(HttpServletRequest request, HttpServletResponse response) { String fileName = "abc.docx";// 文件名 if (fileName != null) { //设置文件路径 File file = new File("F://abc.docx"); //File file = new File(realPath , fileName); if (file.exists()) { // 设置强制下载不打开 response.setContentType("application/force-download"); // 6.添加http头信息 // 因为fileName的编码格式是UTF-8 但是http头信息只识别 ISO8859-1 的编码格式 // 因此要对fileName重新编码 try { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } return "下载成功"; } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } return "下载失败"; } @PostMapping("/multifileUploads") public String upload(@RequestParam("multifile") MultipartFile[] multifile) throws IOException { if (multifile.length > 0) { for (MultipartFile file : multifile) { // 获取文件原名 String originalFilename = file.getOriginalFilename(); // 创建一个新的File对象用于存放上传的文件 File fileNew = new File("F:\" + originalFilename); file.transferTo(fileNew); } return "上传完成"; } return "上传失败!"; }



