/**
* Copyright © 2015-2050 谦亨科技 All rights reserved.
*/
package com.qhwl.admin.cms.web;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.qhwl.admin.cms.entity.CmsFile;
import com.qhwl.admin.cms.service.CmsFileService;
import com.qhwl.admin.hyyj.entity.CmsVideoSet;
import com.qhwl.admin.hyyj.service.CmsVideoSetService;
import com.qhwl.common.config.Global;
import com.qhwl.common.fileupload.OSSUnit;
import com.qhwl.common.mapper.JsonMapper;
import com.qhwl.common.persistence.Page;
import com.qhwl.common.utils.FileUtils;
import com.qhwl.common.utils.StringUtils;
import com.qhwl.common.web.BaseController;
/**
* cms文件管理Controller
*
* @author ztj
* @version 2017-02-21
*/
@Controller
@RequestMapping(value = "${adminPath}/cms/cmsFile")
public class CmsFileController extends BaseController {
@RequestMapping(value = "fileUpload")
@ResponseBody
public Map<String, String> fileUpload(@RequestParam(value = "picUrl") MultipartFile picUrl, @RequestParam String x,
@RequestParam String y, @RequestParam String w, @RequestParam String h) throws IOException {
String refileName = "";
if (picUrl != null) {
String fileName = picUrl.getOriginalFilename();
byte[] data = null;
InputStream fis = null;
ImageInputStream iis = null;
try {
if (picUrl.getBytes().length < 5242880 && FileUtils.isImg(fileName)) {
data = picUrl.getBytes();
String imageFormat = FileUtils.fileSuff(fileName);
fis = picUrl.getInputStream();
Iterator it = ImageIO.getImageReadersByFormatName(imageFormat);
ImageReader reader = (ImageReader) it.next();
// 获取图片流
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
// 定义一个矩形
Rectangle rect = new Rectangle(Integer.parseInt(x), Integer.parseInt(y), Integer.parseInt(w),
Integer.parseInt(h));
// 提供一个 BufferedImage,将其用作解码像素数据的目标。
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
// 保存新图片
File file = multipartToFile(picUrl,imageFormat);
ImageIO.write(bi, imageFormat, file);
refileName = OSSUnit.uploadObject2OSS(file);
} else {
logger.debug("文件大于5M或者文件类型不正确!");
}
} catch (IOException e1) {
logger.debug("文件上传失败" + e1);
} finally {
if (fis != null)
fis.close();
if (iis != null)
iis.close();
}
}
Map<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put("imagePath", refileName);
return jsonMap;// 返回json串
}
/**
* MultipartFile 转换成File
*
* @param multfile 原文件类型
* @return File
* @throws IOException
*/
private File multipartToFile(MultipartFile multfile,String suff) throws IOException {
CommonsMultipartFile cf = (CommonsMultipartFile)multfile;
//这个myfile是MultipartFile的
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
//手动创建临时文件
if(file.length() < 5242880){
String fileName = file.getName();
int index= fileName.lastIndexOf(".");
if(index!=-1){
fileName = fileName.substring(0,index + 1)+suff;
}
File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
fileName);
multfile.transferTo(tmpFile);
return tmpFile;
}
return file;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jcrop+ajaxuploadfile图片裁切上传oss
共27个文件
html:7个
js:5个
css:4个
需积分: 9 8 下载量 160 浏览量
2017-12-18
17:19:03
上传
评论
收藏 288KB RAR 举报
温馨提示
jcrop+ajaxuploadfile文件裁切上传oss:资源为实际代码示例 实际运行还需要根据开发环境做相应调整 详细介绍请参考博客
资源推荐
资源详情
资源评论
收起资源包目录
jcrop+ajaxuploadfile.rar (27个子文件)
jcrop+ajaxuploadfile文件裁切上传oss
css
Jcrop.gif 329B
jquery.Jcrop.css 3KB
jquery.Jcrop.min.css 2KB
demos
styling.html 3KB
img
glyphicons-halflings.png 56KB
tutorial5.html 8KB
tutorial1.html 2KB
crop.php 3KB
non-image.html 2KB
tutorial3.html 3KB
tutorial2.html 2KB
demo_files
sagomod.jpg 16KB
sago.jpg 18KB
pool.jpg 24KB
demos.css 3KB
sagomod.png 77KB
main.css 124KB
tutorial4.html 6KB
js
jquery.min.js 91KB
jquery.Jcrop.min.js 22KB
jquery.color.js 16KB
jquery.Jcrop.js 41KB
CmsFileController.java 5KB
ajaxfileupload.js 10KB
fileForm.jsp 5KB
bower.json 463B
OSSUnit.java 5KB
共 27 条
- 1
资源评论
程序猿吉良吉影
- 粉丝: 1088
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功