关于图片上传与下载(Java)
图片的上传
package com.upload;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
@SuppressWarnings("serial")
public class DoUploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
// 新建一个SmartUploaf对象
SmartUpload su = new SmartUpload();
// 得到PageContext对象
PageContext pageContext = JspFactory
.getDefaultFactory()
.getPageContext(this, request, response, null, true, 8192, true);
// 上传初始化
su.initialize(pageContext);
try {
// 上传文件
su.upload();
// 将文件保存到指定的目录,并返回上传文件数
int count = su.save("/js");
out.print("<center>"+count + "个文件上传成功!<br>" + su.toString()+"</center>");
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out
.print(" <style type='text/css'>"
+ "<!--"
+ ".STYLE3 {"
+ " font-size: 12px"
+ "}"
+ "-->"
+ "</style>"
+ " <table width='600' border='1' id='tableInfo'"
+ " style='border: 1px solid #CCCCCC; '"
+ " cellpadding='0' cellspacing='0' align='center'>");
// 逐一提取上传文件信息,同事保存文件
for (int i = 0; i < su.getFiles().getCount(); i++) {
File file = su.getFiles().getFile(i);
// 显示当前文件信息
out
.print(" <tr>"
+ " <td width='28%'>"
+ " <div align='right' class='STYLE3'>"
+ " 表单项名(getFieldName): </div> </td>"
+ " <td width='72%'>"
+ file.getFieldName()
+ " </td>"
+ " </tr>"
+ " <tr>"
+ " <td>"
+ " <div align='right' class='STYLE3'>"
+ " 文件长度(getSize): </div> </td>"
+ " <td>"
+ file.getSize()
+ " </td>"
+ " </tr>"
+ " <tr>"
+ " <td>"
+ " <div align='right' class='STYLE3'>"
+ " 文件名(getFileName): </div> </td>"
+ " <td>"
+ file.getFileName()
+ " </td>"
+ " </tr>"
+ " <tr>"
+ " <td>"
+ " <div align='right' class='STYLE3'>"
+ " 文件扩展名(getFileExt): </div> </td>"
+ " <td>"
+ file.getFileExt()
+ " </td>"
+ " </tr>"
+ " <tr>"
+ " <td>"
+ " <div align='right' class='STYLE3'>"
+ " 文件全名(getFilePathName): </div> </td>"
+ " <td>"
+ file.getFilePathName()
+ " </td>" + " </tr>");
}
out.print(" </table>");
}
}
图片的下载
package com.upload;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
@SuppressWarnings("serial")
public class DoDownLoadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
//得到 要下载的文件的名称
String fileName=new String(request.getParameter("fileName").getBytes("ISO8859-1"),"GB2312");
//得到SmartUpload对象
SmartUpload su=new SmartUpload();
//得到PageContext对象
PageContext pagecontext=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);
//初始化su
su.initialize(pagecontext);
//下载
su.setContentDisposition(null);
try {
su.downloadFile("/js/"+fileName);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}