springboot 上传文件的代码

后端:
@RequestMapping("/upload")
public String upload(@RequestParam(“pic”)MultipartFile file, HttpServletRequest request){
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
/System.out.println(“fileName–>” + fileName);
System.out.println(“getContentType–>” + contentType);
/
//String filePath = request.getSession().getServletContext().getRealPath(“imgupload/”);
String filePath = “D:/imgup”;
try {
this.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
// TODO: handle exception
}

return "success";

}
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}

前台:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39638459/article/details/84899751
今日推荐