码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-03 11:32:11      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   java   sp   文件   



/**
 * 
 */
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

标签:des   blog   http   io   ar   os   java   sp   文件   

原文地址:http://blog.csdn.net/huahuagongzi99999/article/details/40737529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!