SpringMVC 文件上传下载-阿里云开发者社区

开发者社区> 云计算> 正文
登录阅读全文

SpringMVC 文件上传下载

简介: 多文件上传 public static List imageUpload(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception { CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.

多文件上传

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();
}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章