//index.jsp
<%@ page contentType="text/html;charset=GBK"%> <html> <head> <title>File Upload</title> </head> <body> <font size=5 color=#FF0000> <b>文件上传----使用jspsmart upload组件</b> </font> <br> <form action="uploadfile" method="post" enctype="multipart/form-data"> <p> 文件名称: <input type="file" name="file1" size="20" maxlength="80"> </p> <p> 文件名称: <input type="file" name="file2" size="20" maxlength="80"> </p> <p> 文件名称: <input type="file" name="file3" size="20" maxlength="80"> </p> <p> 上传路径: <input type="text" name="path" size="30" maxlength="50"> <br> </p> <p> 附加内容: <input type="text" name="other" size="30" maxlength="50"> </p> <p> <input type="submit" value="上传"> <input type="reset" value="重置"> </p> </form> <font size=5 color=#FF0000> <b>文件下载----使用jspsmart upload组件</b> </font> <br> <form action="downloadfile" method="post"> <p> 下载文件的名称: <input type="text" name="downloadFileName" size="20" maxlength="80"> </p> <input type="submit" value="下载"> </body> </form> </html>
==================================================
// Servlet: ServletUpload.java 上传
package wit.ou; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; /** * 描述:上传servlet * @author 2009-3-4 转载 http://www.blogjava.net/hijackwust/archive/2007/08/22/138598.html * */ public class ServletUpload extends HttpServlet { private ServletConfig config; final public void init(ServletConfig config) throws ServletException { this.config = config; } protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY BGCOLOR='white'>"); out.println("<H1>jspSmartUpload : Servlet Sample</H1>"); out.println("<HR>"); // 变量定义 int count = 0; //创建一个SmartUpload类 SmartUpload mySmartUpload = new SmartUpload(); try { //初始化 mySmartUpload.initialize(config, request, response); //上传 mySmartUpload.upload(); for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) { com.jspsmart.upload.File myfile = mySmartUpload.getFiles().getFile(i); String fileName = myfile.getFileName(); //保存 count = mySmartUpload.save("/upload"); //count = mySmartUpload.save(null); } out.println(count + " file uploaded."); } catch (Exception e) { out.println("Unable to upload the file.<br>"); out.println("Error : " + e.toString()); } //通过 方法 mySmartUpload.getRequest().getParameter(arg0);还可以获取传过来的非file类型的其他值。 out.println("</BODY>"); out.println("</HTML>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
//servlet: ServletDownload.java 下载
package wit.ou; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; public class ServletDownload extends HttpServlet { private ServletConfig config; final public void init(ServletConfig config) throws ServletException { this.config = config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String temp_p =request.getParameter("downloadFileName"); byte[] temp_t=temp_p.getBytes("ISO8859_1"); String fileName=new String(temp_t,"GBK"); SmartUpload mySmartUpload = new SmartUpload(); try { //初始化 mySmartUpload.initialize(config, request, response); //设置不自动打开 mySmartUpload.setContentDisposition(null); mySmartUpload.downloadFile("/upload/"+fileName); } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
其中上传的时候 还可以通过jar包提供的方法设置一些上传的参数!
下载文件常用的方法
1、setContentDisposition
作用:将数据追加到MIME文件头的CONTENT-DISPOSITION域。jspSmartUpload组件会在返回下载的信息时自动填写MIME文件头的CONTENT-DISPOSITION域,如果用户需要添加额外信息,请用此方法。
原型:public void setContentDisposition(String contentDisposition)
其中,contentDisposition为要添加的数据。如果contentDisposition为null,则组件将自动添加"attachment;",以表明将下载的文件作为附件,结果是IE浏览器将会提示另存文件,而不是自动打开这个文件(IE浏览器一般根据下载的文件扩展名决定执行什么操作,扩展名为doc的将用word程序打开,扩展名为pdf的将用acrobat程序打开,等等)。
2、downloadFile
作用:下载文件。
原型:共有以下三个原型可用,第一个最常用,后两个用于特殊情况下的文件下载(如更改内容类型,更改另存的文件名)。
① public void downloadFile(String sourceFilePathName)
其中,sourceFilePathName为要下载的文件名(带目录的文件全名)
② public void downloadFile(String sourceFilePathName,String contentType)
其中,sourceFilePathName为要下载的文件名(带目录的文件全名),contentType为内容类型(MIME格式的文件类型信息,可被浏览器识别)。
③ public void downloadFile(String sourceFilePathName,String contentType,String destFileName)
其中,sourceFilePathName为要下载的文件名(带目录的文件全名),contentType为内容类型(MIME格式的文件类型信息,可被浏览器识别),destFileName为下载后默认的另存文件名。