实现图片的上传和下载
- 1.图片的上传
@ResponseBody
@RequestMapping("/uploadImgList")
public R uploadPictureList(@RequestParam(value="rtid",required=false)Integer rtid,
@RequestParam(value="imgtype",required=false)Integer imgtype,
@RequestParam(value="uploadFile",required=false)MultipartFile[] uploadFile,
HttpServletRequest request, HttpServletResponse response){
System.out.println("房型:"+rtid);
System.out.println("图型:"+imgtype);
System.out.println("========"+Arrays.toString(uploadFile));
File targetFile=null;
String msg="";
int code=1;
List imgList=new ArrayList();
int sum=0;
if (uploadFile!=null && uploadFile.length>0) {
for (int i = 0; i < uploadFile.length; i++) {
System.out.println(uploadFile[i]);
sum=sum+1;
String fileName=uploadFile[i].getOriginalFilename();
System.out.println(fileName);
if(fileName!=null&&fileName!=""){
String returnUrl =RiShunConstant.PATH_ROOM_PICTURE;
System.out.println(returnUrl);
String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());
System.out.println(fileF);
fileName=rtid.toString()+imgtype.toString()+sum+fileF;
System.out.println(fileName);
File file1 =new File(returnUrl);
if(!file1 .exists() && !file1 .isDirectory()){
file1 .mkdir();
}
targetFile = new File(file1, fileName);
try {
uploadFile[i].transferTo(targetFile);
msg="上传成功";
} catch (Exception e) {
e.printStackTrace();
msg="上传失败";
}
}
}
}
return R.ok(msg);
}
- 2.图片的下载
@RequestMapping("/title_download")
public void titleDownload(Integer rtId,Integer imgtype,HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("房型:"+rtId);
System.out.println("图型:"+imgtype);
int sum=0;
List<String> imgUrls=new ArrayList<String>();
for (int i = 1; i <101 ; i++) {
sum=sum+1;
String path=rtId.toString()+imgtype.toString()+sum+".jpg";
String realpath=RiShunConstant.PATH_ROOM_PICTURE+path;
System.out.println(realpath);
File file = new File(realpath);
if(file.exists()){
String realpath1="file:///"+realpath;
System.out.println(realpath1);
imgUrls.add(realpath1);
}else{
break;
}
}
try {
String downloadFilename = System.currentTimeMillis()+"_图片.zip";
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
String [] files =imgUrls.toArray(new String[imgUrls.size()]);
if(null != files && files.length > 0) {
for (int i = 0; i < files.length; i++) {
System.out.println("====="+files[i]);
String substring3 = files[i].substring(files[i].length()-7);
System.out.println(substring3);
URL url = new URL(files[i]);
zos.putNextEntry(new ZipEntry(substring3));
InputStream fis = url.openConnection().getInputStream();
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
e.getMessage();
System.out.println("下载图片出异常了");
} catch (IOException e) {
e.getMessage();
System.out.println("下载图片出异常了");
}
}