工作总结之----生成缩略图并上传
2018-01-02 14:14
134 查看
/**
* 保存项目
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/saveData", method = RequestMethod.POST)
@ResponseBody
public HashMap saveDataNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap map = new HashMap();
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
//缩略图
MultipartFile multipartFile1 = multipartHttpServletRequest.getFile("file1");
Long si = multipartFile1.getSize();
if(si>0){
Properties properties = ConfigUtil.PROPERTIES;
String path = properties.getProperty("imageResourcrsPath");
path = path + "/image";
// 获取上传的图片文件
String fileName = multipartFile1.getOriginalFilename();
File file = new File(fileName);
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
// 防止出现重名文件--uuid重命名
File newFile = FileUtil.renameFile(file);
// 服务器存的真实名字
String newFileName = newFile.getName();
// 服务器路径(路径+真实名字)
StringBuffer newPaths = new StringBuffer(path);
newPaths.append("\\");
newPaths.append(newFileName);
// 路径+真实名字
String newPath = newPaths.toString();
// 服务器中文件
File targetFile = new File(path, newFileName);
try {
multipartFile1.transferTo(targetFile);
processUploadedFile(newPaths.toString(),newFileName);
} catch (Exception a) {
a.printStackTrace();
}
StringBuffer thuPath = new StringBuffer();
String str = "/resource/image/small-";
thuPath = thuPath.append(str).append(newFileName);
//图片服务器存储路径
String thumbnailPath=thuPath.toString();
//图片名称
String thumbnailName="small-" + fileName;
Thumbnail thum =new Thumbnail();
thum.setThumbnailPath(thumbnailPath);
thum.setThumbnailName(thumbnailName);
newsListService.insertSelective(thum);//保存缩略图信息
}
Integer code = 1;
map.put("code", code);
return map;
}
/**
* <p>
* Description: 上传文件重命名
* </p>
*
* @param file
* 文件名
* @return 文件
* @author : gaoying
* @update :
* @date : 2015-7-26
*/
public static File renameFile(File file) {
String body = "";
String ext = "";
Date date = new Date();
int pot = file.getName().lastIndexOf(".");
if (pot != -1) {
// body = date.getTime() + "";
body = UUID.randomUUID().toString().replace("-", "");
ext = file.getName().substring(pot);
} else {
body = (new Date()).getTime() + "";
ext = "";
}
String newName = body + ext;
file = new File(file.getParent(), newName);
return file;
}
//生成缩略图的操作
private void processUploadedFile(String path, String name) throws Exception {
long time = System.currentTimeMillis();
GMOperation op = new GMOperation();
//待处理图片的绝对路径
op.addImage(path);
//图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰 s
op.quality(100.0);
//width 和height可以是原图的尺寸,也可以是按比例处理后的尺寸
op.addRawArgs("-resize", "500");
//宽高都为100
//op.addRawArgs("-resize", "100x100");
op.addRawArgs("-gravity", "center");
//op.resize(100, null);
//从属性文件中读取缩略图的保存路径
Properties properties = ConfigUtil.PROPERTIES;
String smallpath = properties.getProperty("imageResourcrsPath");
smallpath = smallpath + "/image";
File smallFile = new File(smallpath);
if(!smallFile.exists()) {
smallFile.mkdir();
}
op.addImage(smallFile.getAbsolutePath() + "/" + "small-" + name);
// 如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
ConvertCmd convert = new ConvertCmd(true);
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
//linux下不要设置此值,不然会报错
convert.setSearchPath(properties.getProperty("thumbnailPath"));
}
convert.run(op);
//压缩图片保存
}
* 保存项目
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/saveData", method = RequestMethod.POST)
@ResponseBody
public HashMap saveDataNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap map = new HashMap();
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
//缩略图
MultipartFile multipartFile1 = multipartHttpServletRequest.getFile("file1");
Long si = multipartFile1.getSize();
if(si>0){
Properties properties = ConfigUtil.PROPERTIES;
String path = properties.getProperty("imageResourcrsPath");
path = path + "/image";
// 获取上传的图片文件
String fileName = multipartFile1.getOriginalFilename();
File file = new File(fileName);
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
// 防止出现重名文件--uuid重命名
File newFile = FileUtil.renameFile(file);
// 服务器存的真实名字
String newFileName = newFile.getName();
// 服务器路径(路径+真实名字)
StringBuffer newPaths = new StringBuffer(path);
newPaths.append("\\");
newPaths.append(newFileName);
// 路径+真实名字
String newPath = newPaths.toString();
// 服务器中文件
File targetFile = new File(path, newFileName);
try {
multipartFile1.transferTo(targetFile);
processUploadedFile(newPaths.toString(),newFileName);
} catch (Exception a) {
a.printStackTrace();
}
StringBuffer thuPath = new StringBuffer();
String str = "/resource/image/small-";
thuPath = thuPath.append(str).append(newFileName);
//图片服务器存储路径
String thumbnailPath=thuPath.toString();
//图片名称
String thumbnailName="small-" + fileName;
Thumbnail thum =new Thumbnail();
thum.setThumbnailPath(thumbnailPath);
thum.setThumbnailName(thumbnailName);
newsListService.insertSelective(thum);//保存缩略图信息
}
Integer code = 1;
map.put("code", code);
return map;
}
/**
* <p>
* Description: 上传文件重命名
* </p>
*
* @param file
* 文件名
* @return 文件
* @author : gaoying
* @update :
* @date : 2015-7-26
*/
public static File renameFile(File file) {
String body = "";
String ext = "";
Date date = new Date();
int pot = file.getName().lastIndexOf(".");
if (pot != -1) {
// body = date.getTime() + "";
body = UUID.randomUUID().toString().replace("-", "");
ext = file.getName().substring(pot);
} else {
body = (new Date()).getTime() + "";
ext = "";
}
String newName = body + ext;
file = new File(file.getParent(), newName);
return file;
}
//生成缩略图的操作
private void processUploadedFile(String path, String name) throws Exception {
long time = System.currentTimeMillis();
GMOperation op = new GMOperation();
//待处理图片的绝对路径
op.addImage(path);
//图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰 s
op.quality(100.0);
//width 和height可以是原图的尺寸,也可以是按比例处理后的尺寸
op.addRawArgs("-resize", "500");
//宽高都为100
//op.addRawArgs("-resize", "100x100");
op.addRawArgs("-gravity", "center");
//op.resize(100, null);
//从属性文件中读取缩略图的保存路径
Properties properties = ConfigUtil.PROPERTIES;
String smallpath = properties.getProperty("imageResourcrsPath");
smallpath = smallpath + "/image";
File smallFile = new File(smallpath);
if(!smallFile.exists()) {
smallFile.mkdir();
}
op.addImage(smallFile.getAbsolutePath() + "/" + "small-" + name);
// 如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
ConvertCmd convert = new ConvertCmd(true);
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
//linux下不要设置此值,不然会报错
convert.setSearchPath(properties.getProperty("thumbnailPath"));
}
convert.run(op);
//压缩图片保存
}
相关文章推荐
- 『ASP.NET 2.0』FileUpload图片上传自动生成缩略图
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- asp.net上传图片并自动生成缩略图和给图片水印
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- ASP.Net 上传图片并生成高清晰缩略图
- Yii图片上传生成缩略图
- Poapor远程上传组件 v1.1(自动生成缩略图)
- PHPDisk 4.0 上传PNG文件时无法生成缩略图的BUG
- asp.net2.0 上传图片(FileUpload控件) 并生成缩略图
- 上传图片 并生成缩略图的 例子 C#.net
- asp.net上传图片生成缩略图
- asp.net上传图片并同时生成缩略图
- 上传图片生成缩略图
- 上传图片时生成缩略图,可以自定义图片尺寸
- struts2 或 spring mvc上传图片生成高清缩略图,亲测可以用哦!
- C# 2005 webform上传图片并生成缩略图
- 上传图片生成缩略图,水印【转】
- [导入]使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- struts2 或 spring mvc上传图片生成高清缩略图,亲测可以用哦!