折腾了我两天的使用JSPSmartUpload控件进行文件的上传和下载问题终于被解决了,现在将要注意的几个点整理出来,方便自己以后查询的同时分享给大家(内容有参考网上其他博客),希望以后文件的上传和下载不再困扰大家。
一、为了方便将上传和下载的代码放到一个JSP页面中,方便测试和学习,代码如下:
<%@ page language="java" contentType="text/html;charset=GBK" import="java.util.*"%>
<html>
<head>
<title>File Upload and Download</title>
</head>
<body>
<font size=5 color=#FF0000> <b>文件上传----使用jspsmart upload组件</b> </font>
<br>
<form action="upload.do" 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="submit" value="上传">
<input type="reset" value="重置">
</p>
</form>
<font size=5 color=#FF0000> <b>文件下载----使用jspsmart upload组件</b> </font>
<br>
<form action="download.do" method="post">
<p>
下载文件的名称: <input type="text" name="downloadFileName" size="20" maxlength="80">
</p>
<input type="submit" value="下载">
</form>
</body>
</html>
代码说明:
1、上传时编码格式必须为GBK格式,下载时可以为GBK也可以为UTF-8格式。
2、上传时<input type="file" name="file1" size="20" maxlength="80">中的name的名字可以为任意字符串(“”都可以),但必须指定name属性,后台处理时不通过name属性获取文件路径及文件名。
3、上传时<input type="file" name="file1" size="20" maxlength="80">的个数不限,即可以一次上传多个文件,但多个文件对应的file的name属性值必须不一样。
4、点击上传按钮,可以将选中的多个文件一次上传到服务器端,如果没选文件,则不进行上传。
5、本测试中使用输入要下载的文件名的方式进行文件下载,点击下载按钮实现文件的下载。
5、两个form表单对应的action可以是任何控制器类,此处使用两个Servlet,方便实现,容易测试(其他控制器类的移植是一样的,核心代码不变)。
6、文件上传时的form表单中必须有<form action="upload.do" method="post"enctype="multipart/form-data">
二、文件上传Servlet类ServletUpload.java的实现如下:
package edu.uestc.updown;
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 ServletUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
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 mySmartUpload = new SmartUpload();
try {
//Smart上传三句核心代码
mySmartUpload.initialize(config, request, response);
mySmartUpload.upload();
count = mySmartUpload.save("/upload");
out.println(count + " file uploaded.");
} catch (Exception e) {
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println("</BODY>");
out.println("</HTML>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
代码说明:
1、该ServletUpload类对应的JSP页面的编码格式必须为GBK格式(易错点)。
2、count = mySmartUpload.save("/upload");中的save()方法的参数是文件上传到服务器端的路径,相对于工程名(WebRoot)的路径,count表示总共上传的文件的个数。
3、上传过程和JSP页面的file中的name属性值没关系,而是在内部进行提取处理的。
三、文件下载Servlet类ServletDownload.java的实现如下:
package edu.uestc.updown;
import java.io.IOException;
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 static final long serialVersionUID = 1L;
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String fileName= (String)request.getParameter("downloadFileName");
SmartUpload mySmartUpload = new SmartUpload();
try {
//Smart下载三句核心代码
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);
}
}
代码说明:
1、如果下载的JSP页面的编码格式是GBK,则在代码中需要加入一句request.setCharacterEncoding("GBK"); 如果下载的JSP页面的编码格式是UTF-8,则不需要这一句转码语句。
2、mySmartUpload.downloadFile("/upload/" + fileName);参数要和上传时候的文件路径保持一致。
总结:通过以上三点就可以实现所有文件的上传和下载,使用时注意几个关键点。JSPSmartUpload控件最大的优点是使用简单,操作方便。
使用jar包的链接为:http://download.csdn.net/detail/liuhaolzjtu/4574428