SpringMVC上传图片并压缩及剪切demo
/**
*
*/
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");
r
SpringMVC上传图片并压缩及剪切demo的更多相关文章
- 利用webuploader插件上传图片文件,完整前端示例demo,服务端使用SpringMVC接收
利用WebUploader插件上传图片文件完整前端示例demo,服务端使用SpringMVC接收 Webuploader简介 WebUploader是由Baidu WebFE(FEX)团队开发的一 ...
- 分享图片压缩上传demo,可以选择一张或多张图片也可以拍摄照片
2016-08-05更新: 下方的代码是比较OLD的了,是通过js进行图片的剪切 旋转 再生成,效率较低. 后来又整合了一个利用native.js本地接口的压缩代码 ,链接在这 .页面中有详细的说明, ...
- 160920、springmvc上传图片不生成临时文件
springMVC上传图片时候小于10k不会再临时目录里面生成临时文件,需要增加一个配置 <property name="maxInMemorySize" value=&qu ...
- SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html
SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...
- SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片
SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片 在上一篇文章中,我们介绍了< SpringMVC上传图片的常规上传方法 >.本文接着第一 ...
- SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
原文地址:https://blog.csdn.net/chenchunlin526/article/details/70945877 SpringMVC上传图片总结(1)---常规方法进行图片上传,使 ...
- springmvc上传图片到Tomcat虚拟目录
一.简介 通过把文件上传到tomcat的虚拟目录,实现代码和资源文件分开. 二.环境 spring+springmvc+mybatis 三.代码实现 1.导入文件上传的jar <dependen ...
- springmvc上传图片并显示图片--支持多图片上传
实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...
- php图片水印添加,压缩,剪切的封装类
php对图片文件的操作主要是利用GD库扩展.当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码.当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有 ...
随机推荐
- HTML5 input事件检测输入框变化[转载]
原文:http://www.linuxidc.com/Linux/2015-07/119984.htm 之前一直用change事件来监听输入框内容是否发生变化,只有当输入框失去焦点时才会触发,没想到h ...
- html 自定义标签使用实现方法
通过指定html命名空间的名字来定义自定义标签:默认的一些标签p div等都在html默认的命名空间下.而自定义的标签可以放在自定义的命名空间下,可通过xmlns:命名空间名 来指定,而自定义标签需要 ...
- js跨域问题解决方案
跨域:当协议.域名.端口号任何一个不相同时,叫称为跨域. HTML5 CORS(cross-origin-resource-sharing)跨域资源共享: 原理:当需要访问跨域的资源时,可以通 ...
- J1001.Java原生桌面及Web开发浅谈
自从Java问世以来,在服务端开发方面取得了巨大的发展.但是在桌面/Web开发方面,一直没有得到大的发展.从最初的AWT,到Swing,再到JavaFX,Java从来没有在桌面/Web解决方案中取得重 ...
- 自定义Base16加密
自定义Base16加 ...
- Git(3)----Eclipse上Git插件使用技巧
转载:http://blog.csdn.net/qq_33066205/article/details/56675704 一_安装EGIT插件 http://download.eclipse.org/ ...
- 《Linux命令行与shell脚本编程大全》 第八章管理文件系统
8.1 探索linux文件系统 8.1.1 基本的Linux文件系统 ext:最早的文件系统,叫扩展文件系统.使用虚拟目录操作硬件设备,在物理设备上按定长的块来存储数据. 用索引节点的系统来存放虚拟目 ...
- [转载] java多线程学习-java.util.concurrent详解(一) Latch/Barrier
转载自http://janeky.iteye.com/blog/769965 Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent, 提供了大量高级工具,可 ...
- Struts2-整理笔记(三)结果处理跳转、获得servletAPI原生
在struts2中有很多种跳转方式如下是在struts-default.xml截取的一段源码,常用的跳转有 转发:dispatcher.重定向:redirect.转发到Action:chain.重定向 ...
- 前端框架对于未来web移动端的影响
现在前端框架市场比较乱,各种各样的框架参差不齐,这给我带来了很多困惑,同样是很多朋友的困惑吧!因为前端框架有很多种,对于程序员来说选择学习是非常困难的,不可能有几十上百种都要学习吧,不过最好的办法就是 ...