java产生缩略图

来源:互联网 发布:华为云计算部门效益 编辑:程序博客网 时间:2024/06/07 19:48
package com.testupload.action;


import java.io.IOException;




import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.coobird.thumbnailator.Thumbnails;




import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;


public class UploadAction extends HttpServlet {


private static final long serialVersionUID = 3480867428580380766L;


public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//确定一个文件上传后存放的物理路径
String filePath = request.getServletContext().getInitParameter("upload_filePath");
String current_timestamp = System.currentTimeMillis() + "" ;
System.out.println(filePath);
//使用jspsmartupload相应的api方法 处理上窜的文件
//创建SmartUpload实例
SmartUpload su = new SmartUpload();
//su.getRequest().getParameter("p_id");

//初始化
su.initialize(this.getServletConfig(), request, response);
//设置上传文件大小
su.setMaxFileSize(1024*1024*10);
//设置所有文件的大小
su.setTotalMaxFileSize(1024*1024*100);
//设置允许上传文件类型  多个类型使用逗号隔开
su.setAllowedFilesList("jpg");
su.setCharset("UTF-8");
     try {
     // 上传文件
su.upload();
for (int i = 0; i < su.getFiles().getCount(); i++) {
if(su.getFiles().getFile(i).isMissing()){  //判断用户有没有选择一个图片文件  
continue ;
}else{
su.getFiles().getFile(i).saveAs(filePath+current_timestamp+"-" + (i+1) +".jpg",SmartUpload.SAVE_PHYSICAL);
Thumbnails.of(filePath+current_timestamp+"-" + (i+1) +".jpg")  //处理谁
.size(40, 40) //设置缩略图的大小尺寸
.keepAspectRatio(false) //默认是按照比例缩放的  设置成false 就是不按照比例缩放
.toFile("F:\\upload-app"+java.io.File.separator + current_timestamp+"-" + (i+1)+ "-40x40" +".jpg"); //处理后存放位置和名称
}

}

} catch (SmartUploadException e) {
e.printStackTrace();
}
//生成缩略图


}


}
0 0
原创粉丝点击