上传(贴关键代码):
String suffix =files.getOriginalFilename().substring(files.getOriginalFilename().lastIndexOf("."));
int filesize = 500000;
if (files.getSize() > filesize) {
flag=false;
} else {
String filename = files.getOriginalFilename();
File fileDir = UploadUtils.getImgDirFile();
try {
File newFile = new File(fileDir.getAbsolutePath() + File.separator + filename);
System.out.println(newFile.getAbsolutePath());
files.transferTo(newFile);
} catch (IOException e) {
e.printStackTrace();
return "fail";
}
logoPicPath = UploadUtils.IMG_PATH_PREFIX + "/" + filename;
下载:
@RequestMapping(value = "/downLoad/{filename}")
String downLoad(HttpServletResponse response,@RequestParam("filename") String filename) throws IOException
{
String fileName = filename;
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buff = new byte[1024];
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(new File("这里写本地文件的路径",filename));
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("Download successfully!");
}
catch (Exception e) {
System.out.println("Download failed!");
}
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;
}