文件上传(后台)
@RequestMapping("/uploadPrivate")
public String uploadPrivate(@RequestParam("file") MultipartFile file){
if(file.isEmpty()){
return "上传失败!";
}
String fileName = file.getOriginalFilename();
String filepath = "D:\\文档\\个人文档\\";
File dest = new File(filepath+fileName);
try {
file.transferTo(dest);
return "上传成功!";
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
return "上传失败!";
}
文件下载后台
@RequestMapping("/pubdownload")
public String downloadFile(@RequestParam("pubid")Integer pubid, HttpServletRequest request, HttpServletResponse response) {
/**
* 这个filename应该是从数据库查出来的
*/
String fileName =getFiles.getPublicFileName(pubid) ;//
if (fileName != null) {
//设置文件路径
String realPath = "D:\\文档\\公共文档";
File file = new File(realPath , fileName);
if (file.exists()) {
response.setContentType("application/octet-stream");//
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
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);
}
System.out.println("success");
} 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 null;
}