您的位置:首页 > 编程语言 > Java开发

SpringMVC上传图片并压缩及剪切demo

2017-05-02 10:23 405 查看
/**
*
*/
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: