servlet上传图片生成缩略图为什么会变模糊
本帖最后由 freeman932 于 2013-10-08 22:58:15 编辑
代码如下,发现上传后的缩略图比原图模糊了很多
package servlet;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/** * Servlet implementation class for Servlet: UploadServlet * */
public class UploadServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
//File tmpDir = null;// 初始化上传文件的临时存放目录
//File saveDir = null;// 初始化上传文件后的保存目录
public UploadServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException,
IOException {
doPost(request, response);
}
@SuppressWarnings({ "unchecked", "deprecation" })
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");//设置输出编码格式
request.setCharacterEncoding("UTF-8"); //设置输入编码格式
PrintWriter out = response.getWriter();
//判断表单是否是 enctype="multipart/form-data"
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(5 * 1024 * 1024); // 设定使用内存超过5M时,将产生临时文件并存储于临时目录中。
dfif.setRepository(new File("f:\\download")); // 设定存储临时文件的目录。
ServletFileUpload fileupload = new ServletFileUpload(dfif);
fileupload.setSizeMax(50 * 1024 * 1024); // 设定最大允许上传50M的文件。
//List fileitems = null;
try {
/*fileitems=fileupload.parseRequest(request);
Iterator iter=fileitems.iterator();
while(iter.hasNext()){
FileItem f=(FileItem)iter.next();*/
List<FileItem> files = fileupload.parseRequest(request);//调用 ServletFileUpload 的 parseRequest() 方法, 获取 FileItem 的集合
for (FileItem f : files)&nbs