SpringMVC 文件上传下载

多文件上传

public static List<String> imageUpload(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception {
CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
// 上文图片路径
List<String> listFile = new ArrayList<>();
String imageUrl = null;
if (cmr.isMultipart(request)) {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request);
List<MultipartFile> listImage = mRequest.getFiles(filename);
if (listImage != null && listImage.size() > 0) {
for (MultipartFile mFile : listImage) {
if (!mFile.isEmpty()) {
String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("."));
suffix = suffix.toLowerCase();
if (suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png")) {
String fileName = UUID.randomUUID().toString();
if (mFile.getSize() < 10000000 && mFile.getSize() > 0) {
PropertiesUtils pro = new PropertiesUtils();
// String path = pro.load("file.properties") +
// "/upload/image";
// 注意 注意 线上获取上传路径地址
String path = request.getServletContext().getRealPath("upload/image/");
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
// 缩略图上传路径 start
String desPath = path + "/thum_" + fileName;
// 压缩图片
Thumbnails.of(mFile.getInputStream()).scale(1f).outputQuality(0.38f).toFile(desPath);
imageUrl = request.getRequestURL().toString();
int index = imageUrl.indexOf("xdfstar") + 7;
String url = imageUrl.substring(0, index);
imageUrl = url + "/upload/image/" + "thum_" + fileName + ".JPEG";
request.setAttribute("imageUrl", imageUrl);
// 存放所有上传图片的路径
listFile.add(imageUrl);
} else {
listFile = null;
}
} else {
}
}
}
} else {
listFile = null;
}
}
return listFile;
}

imgurls = FileUpload.imageUpload("picture", request, response);

下载:

public static void download(HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType,
String realName) throws Exception {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

String ctxPath = request.getSession().getServletContext()
.getRealPath("/")+"upload/image/";

String downLoadPath = ctxPath + storeName;

long fileLength = new File(downLoadPath).length();

response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(realName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}

String contentType = "application/octet-stream";
try {
FileDownload.download(request, response, url, contentType,String.valueOf(new Random().nextInt(10000)+1)+".jpg");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

原文地址:https://www.cnblogs.com/coderdxj/p/6511198.html