/**
*
*/
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的更多相关文章

  1. 利用webuploader插件上传图片文件,完整前端示例demo,服务端使用SpringMVC接收

    利用WebUploader插件上传图片文件完整前端示例demo,服务端使用SpringMVC接收 Webuploader简介   WebUploader是由Baidu WebFE(FEX)团队开发的一 ...

  2. 分享图片压缩上传demo,可以选择一张或多张图片也可以拍摄照片

    2016-08-05更新: 下方的代码是比较OLD的了,是通过js进行图片的剪切 旋转 再生成,效率较低. 后来又整合了一个利用native.js本地接口的压缩代码 ,链接在这 .页面中有详细的说明, ...

  3. 160920、springmvc上传图片不生成临时文件

    springMVC上传图片时候小于10k不会再临时目录里面生成临时文件,需要增加一个配置 <property name="maxInMemorySize" value=&qu ...

  4. SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html

    SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...

  5. SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片

    SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片   在上一篇文章中,我们介绍了< SpringMVC上传图片的常规上传方法 >.本文接着第一 ...

  6. SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest

    原文地址:https://blog.csdn.net/chenchunlin526/article/details/70945877 SpringMVC上传图片总结(1)---常规方法进行图片上传,使 ...

  7. springmvc上传图片到Tomcat虚拟目录

    一.简介 通过把文件上传到tomcat的虚拟目录,实现代码和资源文件分开. 二.环境 spring+springmvc+mybatis 三.代码实现 1.导入文件上传的jar <dependen ...

  8. springmvc上传图片并显示图片--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

  9. php图片水印添加,压缩,剪切的封装类

    php对图片文件的操作主要是利用GD库扩展.当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码.当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有 ...

随机推荐

  1. grid 布局

    display:grid 是一种新的布局方式,旧的布局方式通常有副作用,例如float(需要额外修复浮动)或者inline-block(两个元素之间的空格问题)   把父元素定义为grid,就像表格一 ...

  2. hook in PostgreSQL初探

    HOOK IN POSTGRESQL 初探 前言 众所周知,PostgreSQL具有很好的扩展性,是一个可以"开发"的数据库.在PostgreSQL里面,你可以定制你自己的Type ...

  3. JavaScript系列----数据类型以及传值和传引用

    1.简单数据类型 在JavaScript中简单数据类型分为5种.分别为 Undefined, Null,Boolean,Number,String. Undefined类型Undefined类型只有一 ...

  4. python3随笔第一天

    1.python 语言没有{},注重书写格式,注重空格的使用,书写python程序一定要注意代码对齐,代码格式对齐是python程序书写的生命: 2.python 分支判断格式  if 条件 :  e ...

  5. 快速自检电脑是否被黑客入侵过(Windows版)

    我们经常会感觉电脑行为有点奇怪, 比如总是打开莫名其妙的网站, 或者偶尔变卡(网络/CPU), 似乎自己"中毒"了, 但X60安全卫士或者X讯电脑管家扫描之后又说你电脑" ...

  6. 接受第三方app分享的数据

    前段时间公司项目需要一个需求: 把第三方的app分享的数据接受到自己的apk中, 涉及到的第三方app是: Youtube/Amazon/NetFlix, 这些app通过分享功能把当前的信息分享出去. ...

  7. 前端面试题(3) cookie,sessionStorage和localStorage的区别

    cookie是网站为了标示用户身份存在用户本地终端上的数据(经过加密). cookie数据时钟在同源的http请求中携带(即使不需要),即会在浏览器和服务器之间传递. seeeionStorage和l ...

  8. [转]ORACLE递归查询

    转自:http://www.oracle.com/technetwork/cn/articles/hartley-recursive-086819-zhs.html 递归数据库处理,也称为材料清单 或 ...

  9. JAVA提高十三:Hashtable&Properties深入分析

    最近因为一些琐碎的事情,导致一直没时间写博客,正好今天需求开发完的早,所以趁早写下本文,本文主要学习的是Hashtable的分析,因为上面一篇文章研究的是HashMap,而Hashtable和Hash ...

  10. pyqt5实现打开子窗口

    # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * class Fi ...