SpringMVC上传图片并压缩及剪切demo
2014-11-03 10:52
281 查看
/** * */ package com.up.controller; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.up.constant.SystemConstants; import com.up.util.FileCopy; import com.up.util.ImageUtils; import com.up.util.OperateImage; import com.up.util.UploadUtil; /** * @author hu.shiguo * @time 2014-3-14下午2:53:15 * @description * @version */ @Controller @RequestMapping(value="upload") public class UploadFileController { /** * 图片上传 * @param request * @param response * @param myFile */ @RequestMapping(value = "uploadFile") public void upload(HttpServletRequest request, HttpServletResponse response, @RequestParam MultipartFile myFile) { //要删除文件的所在子目录 String fileFolderpath = request.getParameter("fileFolderpath"); //工程目录 String projectPath = SystemConstants.WEB_ROOT; File files = new File(projectPath); //上传文件保存目录 String uploadFilePath = files.getParent()+File.separator; //String uploadFilePath =files.getPath()+File.separator + "uploadFile"; //上传实际路径 String basePath = uploadFilePath+"uploadFile"+File.separator+"up"+File.separator +fileFolderpath; //未压缩的图片上传至临时文件夹 String tempPath = uploadFilePath+"uploadFile"+File.separator+"up"+File.separator +SystemConstants.URL_TEMP; InputStream is = null ; FileOutputStream os = null; if (!new File(basePath).isDirectory()) { new File(basePath).mkdirs(); } try { //获取上传文件旧名 String name = myFile.getOriginalFilename(); //获取后缀名 String last = name.substring(name.lastIndexOf(".")+1); //上传路径--压缩前 String org = ""; File file = new File(tempPath, System.currentTimeMillis() + new Random(50000).nextInt() + "."+last); is = myFile.getInputStream(); os = new FileOutputStream(file); //上传 UploadUtil.copyFile(is, os); //获取未压缩图片上传后的绝对路径===在临时文件夹目录中 org = file.getAbsolutePath(); System.out.println("org=="+org); //压缩后图片存储路径 String dest = System.currentTimeMillis() + new Random(50000).nextInt() + "."+last; System.out.println("dest=="+dest); //System.out.println("file.getName():"+file.getName()); //方法一:进行压缩 boolean bol1 = ImageUtils.resize(org, basePath+File.separator+dest, 200, 200); //方法二:进行剪切 //返回压缩后的图片名称到前端展示 //先缩放,再裁剪 boolean bol2 = false; if(bol1){ OperateImage o = new OperateImage(basePath+File.separator+dest, 0, 0, 200, 200); o.setSubpath(basePath+File.separator+dest); o.setImageType(last); bol2 = o.cut(); } if(bol1||bol2){ System.out.println("---------"+dest); response.getWriter().write(dest); }else{ FileCopy fc = new FileCopy(); //因为没有压缩,所以将未压缩的文件从临时文件中复制至目标路径下 fc.doMoveFile(file.getName(), tempPath, basePath); response.getWriter().write(file.getName()); } } catch (Exception e) { e.printStackTrace(); }finally{ try{ if (os != null) { os.close(); } if (is != null) { is.close(); } } catch (Exception e) { os = null; is = null; } } } /** * 删除上传图片 * @param request * @param response * @param myFile */ @RequestMapping(value = "deleteFile.html") public void deleteFile(HttpServletRequest request, HttpServletResponse response) { String fileFolderpath = request.getParameter("fileFolderpath")+"/"; String fileName = request.getParameter("fileName"); //工程目录 String webPath = SystemConstants.WEB_ROOT; File files = new File(webPath); //上传文件的目录 String uploadFilePath = files.getParent()+File.separator; //String uploadFilePath =files.getPath()+File.separator + "uploadFile"; String basePath = uploadFilePath+SystemConstants.URL_UPLOADFILE+fileFolderpath+fileName; File file = new File(basePath); if (file.isFile() || file.isDirectory()) { file.delete(); } String str = "true"; response.setContentType("text/xml;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out; try { out = response.getWriter(); out.print(str);// 用于返回对象参数 } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- SpringMVC上传图片并压缩及剪切demo
- SpringMvc图片压缩上传
- 图片工具类(图片上传、压缩、旋转、剪切)
- springmvc图片上传后压缩处理
- 高仿微信上传头像附带压缩,旋转图片,附加demo
- H5技术完美实现调用手机摄像头、相册。图片上传base64,图片压缩、预览、删除以及图片旋转90度的处理--demo。
- MUI - 图片压缩上传demo,可以选择一张或多张图片也可以拍摄照片
- SpringMVC实现简单的图片上传DEMO
- php图像处理类,上传,压缩,添加文字、图片水印
- 图片压缩后上传[Flash]
- 图片上传_ajax上传之实际应用(附上Demo)
- PHP 图片上传及 剪切 代码
- Asp.net MVC 实现图片上传剪切
- Java图片上传压缩处理
- java 上传并压缩图片
- 表单中上传文件、压缩上传图片
- as3与php 上传多张图片demo
- 文件操作类(上传文件、删除文件、图片压缩、图片裁剪、文件读写...)
- 上传图片按比例压缩
- C#也能PS图片,还能为网站Ajax上传图片同时生成微缩图(附Demo)