/** * 上传文件工具类 * @param file * @return */ public Results upload(MultipartFile file) { Results rs = new Results(); String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if(!Arrays.asList("jpg","png","bmp","doc","docx","pdf").contains(suffix)){ rs.setStatusCode(Contents.requestFail); rs.setStatusMsg("上传文件格式不正确!"); return rs; } try { //文件存放路径 String filePath = CaseStringUtils.getDateForString(System.currentTimeMillis()+"", "yyyy") //年份 + "/" + CaseStringUtils.getDateForString(System.currentTimeMillis()+"", "MM") //月份 + "/" + CaseStringUtils.getDateForString(System.currentTimeMillis()+"", "dd"); File tmpPath = new File(fileTmpPath +"/"+ filePath); if (!tmpPath.exists()){//若此目录不存在,则创建 tmpPath.mkdirs(); log.info("创建目录路径为:"+ filePath); } if (null != file) { log.info("保存上传文件:"+ file.getName()); //生成文件的名称和缩略图的文件名称 Long temp = System.nanoTime(); String fileSaveName = temp + "." + suffix; String localPath = fileTmpPath +"/"+ filePath +"/"+ fileSaveName; File dest = new File(localPath); file.transferTo(dest); Map<String, Object> map = new HashMap<String, Object>(); map.put("suffix",suffix); map.put("filePath",filePath); rs.setResMap(map); rs.setStatusCode(Contents.requestSuccess); rs.setStatusMsg("上传文件成功"); rs.setTimestamp(String.valueOf(System.currentTimeMillis())); return rs; }else{ rs.setStatusCode(Contents.requestSuccess); rs.setStatusMsg("没有上传文件"); return rs; } }catch(Exception e){ e.printStackTrace(); rs.setStatusCode(Contents.requestFail); rs.setStatusMsg("上传失败"); } log.info(JSON.toJSONString(rs)); return rs; } /** * 将文件路径转换成FileItem * @param filePath * @return */ public static FileItem createFileItem(String filePath) { FileItemFactory factory = new DiskFileItemFactory(16, null); //String textFieldName = "textField"; int num = filePath.lastIndexOf("."); String extFile = filePath.substring(num); FileItem item = factory.createItem(filePath, "text/plain", true, filePath + extFile); File newfile = new File(filePath); int bytesRead = 0; byte[] buffer = new byte[8192]; try { FileInputStream fis = new FileInputStream(newfile); OutputStream os = item.getOutputStream(); while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } return item; }
/** * 描述:Test * * @author 何志鹏 * @ClassName:Test * @create 2019-09-23 14:54 * Version 1.0 */ @RestController @RequestMapping("/file") public class Test { @Autowired private FileAPIUtils fileAPIUtils; @Autowired private FileFeignClient fileFeignClient; @Value("${file.fileTmpPath}") private String fileTmpPath; 方法一: @RequestMapping("/uploadTest") public Results download()throws Exception{ String path = "D:/13639685_123501617185_2.jpg"; FileItem fileItem = createFileItem(path); MultipartFile mfile = new CommonsMultipartFile(fileItem); Results upload = fileAPIUtils.upload(mfile); return upload; }
方法二:不使用createFileItem这个工具类 直接用DiskFileItem类
@RequestMapping("/uploadTest2") public Results downloads()throws Exception{ File file = new File("C:/tmpdir/15707688186414892.pdf"); DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",MediaType.TEXT_PLAIN_VALUE, true, file.getName()); try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) { IOUtils.copy(input, os); } catch (Exception e) { throw new IllegalArgumentException("Invalid file: " + e, e); } MultipartFile mfile = new CommonsMultipartFile(fileItem); Results upload = fileAPIUtils.upload(mfile); return upload; }
}