先说明我的需求和我的实现方式
首先把上传文件 保存在指定的目录下 然后对其进行缩放 截图 之后想把原图删除,但是因为smartUpload未释放上传文件,因此删除不了。想寻求下解决方案
以下是servlet中的代码
package com.netexpress.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.netexpress.Gravatar.Gravtar;public class UploadImageServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { PrintWriter out = response.getWriter();
response.setCharacterEncoding("gb18030");
response.setHeader("Charset","gb18030");
response.setHeader("Cache-Control", "no-cache");
String filepath="",msg="",ext="",trace="",path="";
String clew = "";
Gravtar g = new Gravtar();
boolean succ=true;
int length = 0;
SmartUpload mySmartUpload = new SmartUpload();
try{
//System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
mySmartUpload.setMaxFileSize(2*1024*1024);
mySmartUpload.setTotalMaxFileSize(2*1024*1024);
mySmartUpload.initialize(getServletConfig(),request,response);
String allowed = "jpg,bmp,gif,jpeg,png";//允许上传文件类型
String denied = "exe,bat,bin,jar,html,js,htm";//不允许上传文件类型
mySmartUpload.setAllowedFilesList(allowed);
mySmartUpload.setDeniedFilesList(denied);
mySmartUpload.upload(); /*String dst_x = mySmartUpload.getRequest().getParameter("dst_x");
System.out.println(dst_x);
String dst_y = mySmartUpload.getRequest().getParameter("dst_y");
System.out.println(dst_y);*/
String dst_w = mySmartUpload.getRequest().getParameter("dst_w");
//System.out.println(dst_w);
String dst_h = mySmartUpload.getRequest().getParameter("dst_h");
//System.out.println(dst_h);
String img_x = mySmartUpload.getRequest().getParameter("img_x");
//System.out.println(img_x);
String img_y = mySmartUpload.getRequest().getParameter("img_y");
//System.out.println(img_y);
/*String img_w = mySmartUpload.getRequest().getParameter("img_w");
System.out.println(img_w);
String img_h = mySmartUpload.getRequest().getParameter("img_h");
System.out.println(img_h);
String left = mySmartUpload.getRequest().getParameter("left");
System.out.println(left);
String top = mySmartUpload.getRequest().getParameter("top");
System.out.println(top);*/
String f = mySmartUpload.getRequest().getParameter("f");
//System.out.println(f);
String width = mySmartUpload.getRequest().getParameter("width");
//System.out.println(width);
String height = mySmartUpload.getRequest().getParameter("height");
//System.out.println(height);
length = mySmartUpload.getFiles().getCount(); //获取上传文件数
//System.out.println(mySmartUpload.getFiles().getCount());
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//System.out.println("进入for循环");
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//System.out.println("进入if体");
ext = myFile.getFileExt().toLowerCase();//格式
//System.out.println(ext);
//全名(绝对路径+名称)
String myFilePath = myFile.getFilePathName();//文件来源全名
//取得文件的大小
int fileSize = myFile.getSize();
//System.out.println(fileSize);
//另存路径
//String jspPath = application.getRealPath(request.getRequestURI());//本JSP文件的服务器绝对路径
String filePath = getServletContext().getRealPath("/upload/GravatarSource/");//本WEB程序根目录服务器绝对路径
String filePath1 = getServletContext().getRealPath("/upload/GravatarZoom/");//保存缩放图片地址
String filePath2 = getServletContext().getRealPath("/upload/GravatarPSC/");
//System.out.println(filePath);
//filePath += "\\UserFiles\\Image\\";
path = filePath;
//要创建的文件最终保存目录
java.io.File fileSource = new java.io.File( filePath );
java.io.File fileZoom= new java.io.File( filePath1 );
java.io.File filePSC = new java.io.File( filePath2 );
// System.out.println("filepath==="+filePath);
//存放原图
if(!fileSource.exists())
fileSource.mkdirs();
trace += ((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+"."+ext);//最终保存路径
//request.setAttribute("Image",trace);
//System.out.println("trace==="+trace);
//存放缩放图
myFile.saveAs(filePath + "\\" + trace);
if(!fileZoom.exists())
fileZoom.mkdirs();
g.saveImageAsJpg(filePath + "\\" + trace, filePath1 + "\\" + trace.replace(ext,"jpg"), Integer.parseInt(width), Integer.parseInt(height));
//存放截图
if(!filePSC.exists())
filePSC.mkdirs();
g.readUsingImageReader(Double.parseDouble(img_x), Double.parseDouble(img_y),Double.parseDouble(dst_w) , Double.parseDouble(dst_h), Double.parseDouble(f), filePath1 + "\\" + trace.replace(ext,"jpg"), filePath2 + "\\" + trace.replace(ext,"jpg"));
//删除原图 暂时没有好方法 smartUpload未释放上传对象 没办法删除 System.gc()不能保证每次都删除
/* java.io.File file2 = new java.io.File(path + "\\" + trace);
System.out.println(path + "\\" + trace);
if(file2.exists()){
boolean b = file2.delete();
System.out.println(b);
}*/
//System.out.println(filePath + "\\" + trace); }
else
{
// 若文件不存在则继续
continue;
}
}
}catch(SmartUploadException sue){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}catch(Exception ex){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}finally{
//最后删除源文件
java.io.File file2 = new java.io.File(path + "\\" + trace);
if(request.getInputStream()!=null){
System.out.println("******************************");
request.getInputStream().close();
}
if(file2.exists()){
boolean b = file2.delete();
System.out.println(b);
}
}
//上传文件长度为0
if(length==0){
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
}
if(succ){
clew = "文件上传成功";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
//System.out.println("上传成功");
//request.getSession().setAttribute("Gravatar",trace);
}else{
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}
}
}
有些乱 不好意思因为一直没有解决问题,所以想g.saveImageAsJpg(filePath + "\\" + trace, filePath1 + "\\" + trace.replace(ext,"jpg"), Integer.parseInt(width), Integer.parseInt(height)); saveImageAsJpg(String inFilePath, String outFilePath,int width, int hight)这个缩放图片的方法第一个参数,改成InputStream流对象,把目的就是想不保存原图,而是直接根据这个流对象进行缩放。saveImageAsJpg(InputStream in, String outFilePath,int width, int hight)但是不晓得怎么在servlet里获取这个流对象。request.getInputStream()
也就是把要上传的文件放到一个InputStream流对象里??????
急急急
首先把上传文件 保存在指定的目录下 然后对其进行缩放 截图 之后想把原图删除,但是因为smartUpload未释放上传文件,因此删除不了。想寻求下解决方案
以下是servlet中的代码
package com.netexpress.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.netexpress.Gravatar.Gravtar;public class UploadImageServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { PrintWriter out = response.getWriter();
response.setCharacterEncoding("gb18030");
response.setHeader("Charset","gb18030");
response.setHeader("Cache-Control", "no-cache");
String filepath="",msg="",ext="",trace="",path="";
String clew = "";
Gravtar g = new Gravtar();
boolean succ=true;
int length = 0;
SmartUpload mySmartUpload = new SmartUpload();
try{
//System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
mySmartUpload.setMaxFileSize(2*1024*1024);
mySmartUpload.setTotalMaxFileSize(2*1024*1024);
mySmartUpload.initialize(getServletConfig(),request,response);
String allowed = "jpg,bmp,gif,jpeg,png";//允许上传文件类型
String denied = "exe,bat,bin,jar,html,js,htm";//不允许上传文件类型
mySmartUpload.setAllowedFilesList(allowed);
mySmartUpload.setDeniedFilesList(denied);
mySmartUpload.upload(); /*String dst_x = mySmartUpload.getRequest().getParameter("dst_x");
System.out.println(dst_x);
String dst_y = mySmartUpload.getRequest().getParameter("dst_y");
System.out.println(dst_y);*/
String dst_w = mySmartUpload.getRequest().getParameter("dst_w");
//System.out.println(dst_w);
String dst_h = mySmartUpload.getRequest().getParameter("dst_h");
//System.out.println(dst_h);
String img_x = mySmartUpload.getRequest().getParameter("img_x");
//System.out.println(img_x);
String img_y = mySmartUpload.getRequest().getParameter("img_y");
//System.out.println(img_y);
/*String img_w = mySmartUpload.getRequest().getParameter("img_w");
System.out.println(img_w);
String img_h = mySmartUpload.getRequest().getParameter("img_h");
System.out.println(img_h);
String left = mySmartUpload.getRequest().getParameter("left");
System.out.println(left);
String top = mySmartUpload.getRequest().getParameter("top");
System.out.println(top);*/
String f = mySmartUpload.getRequest().getParameter("f");
//System.out.println(f);
String width = mySmartUpload.getRequest().getParameter("width");
//System.out.println(width);
String height = mySmartUpload.getRequest().getParameter("height");
//System.out.println(height);
length = mySmartUpload.getFiles().getCount(); //获取上传文件数
//System.out.println(mySmartUpload.getFiles().getCount());
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//System.out.println("进入for循环");
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//System.out.println("进入if体");
ext = myFile.getFileExt().toLowerCase();//格式
//System.out.println(ext);
//全名(绝对路径+名称)
String myFilePath = myFile.getFilePathName();//文件来源全名
//取得文件的大小
int fileSize = myFile.getSize();
//System.out.println(fileSize);
//另存路径
//String jspPath = application.getRealPath(request.getRequestURI());//本JSP文件的服务器绝对路径
String filePath = getServletContext().getRealPath("/upload/GravatarSource/");//本WEB程序根目录服务器绝对路径
String filePath1 = getServletContext().getRealPath("/upload/GravatarZoom/");//保存缩放图片地址
String filePath2 = getServletContext().getRealPath("/upload/GravatarPSC/");
//System.out.println(filePath);
//filePath += "\\UserFiles\\Image\\";
path = filePath;
//要创建的文件最终保存目录
java.io.File fileSource = new java.io.File( filePath );
java.io.File fileZoom= new java.io.File( filePath1 );
java.io.File filePSC = new java.io.File( filePath2 );
// System.out.println("filepath==="+filePath);
//存放原图
if(!fileSource.exists())
fileSource.mkdirs();
trace += ((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+"."+ext);//最终保存路径
//request.setAttribute("Image",trace);
//System.out.println("trace==="+trace);
//存放缩放图
myFile.saveAs(filePath + "\\" + trace);
if(!fileZoom.exists())
fileZoom.mkdirs();
g.saveImageAsJpg(filePath + "\\" + trace, filePath1 + "\\" + trace.replace(ext,"jpg"), Integer.parseInt(width), Integer.parseInt(height));
//存放截图
if(!filePSC.exists())
filePSC.mkdirs();
g.readUsingImageReader(Double.parseDouble(img_x), Double.parseDouble(img_y),Double.parseDouble(dst_w) , Double.parseDouble(dst_h), Double.parseDouble(f), filePath1 + "\\" + trace.replace(ext,"jpg"), filePath2 + "\\" + trace.replace(ext,"jpg"));
//删除原图 暂时没有好方法 smartUpload未释放上传对象 没办法删除 System.gc()不能保证每次都删除
/* java.io.File file2 = new java.io.File(path + "\\" + trace);
System.out.println(path + "\\" + trace);
if(file2.exists()){
boolean b = file2.delete();
System.out.println(b);
}*/
//System.out.println(filePath + "\\" + trace); }
else
{
// 若文件不存在则继续
continue;
}
}
}catch(SmartUploadException sue){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}catch(Exception ex){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}finally{
//最后删除源文件
java.io.File file2 = new java.io.File(path + "\\" + trace);
if(request.getInputStream()!=null){
System.out.println("******************************");
request.getInputStream().close();
}
if(file2.exists()){
boolean b = file2.delete();
System.out.println(b);
}
}
//上传文件长度为0
if(length==0){
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
}
if(succ){
clew = "文件上传成功";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
//System.out.println("上传成功");
//request.getSession().setAttribute("Gravatar",trace);
}else{
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
out.close();
return;
}
}
}
有些乱 不好意思因为一直没有解决问题,所以想g.saveImageAsJpg(filePath + "\\" + trace, filePath1 + "\\" + trace.replace(ext,"jpg"), Integer.parseInt(width), Integer.parseInt(height)); saveImageAsJpg(String inFilePath, String outFilePath,int width, int hight)这个缩放图片的方法第一个参数,改成InputStream流对象,把目的就是想不保存原图,而是直接根据这个流对象进行缩放。saveImageAsJpg(InputStream in, String outFilePath,int width, int hight)但是不晓得怎么在servlet里获取这个流对象。request.getInputStream()
也就是把要上传的文件放到一个InputStream流对象里??????
急急急
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);//取得文件
myFile.saveAs("uploadfile/"+fileName);然后你对这个文件进行操作就行了!
in.close()方法没有执行到 在之前return了