Posted on 2008-10-02 23:16
H2O 阅读(482)
评论(0) 编辑 收藏 所属分类:
java
此例子是基于jspsmartupload组件的,jspsmartupload是一个不错的上传下载组件,但对中文支持不足。若下载的文件名中有汉字,则浏览器在提示另存的文件名时,显示的是一堆乱码,让人看了很不舒服,为此,有人专门修改此组件,做了编码的转换工作,将文件名转换为UTF-8形式的编码形式。我用的是网上修改过的,已经可以支持中文,相信你也可以找到,如果需要,可以联系我,我会在第一时间发给你!
在网上找了很多相关资料,自己也添加了一些js代码,基本实现了动态添加删除多文件上传的功能,如果想要做得更完美,或者把文件上传下载信息存储到数据库等,那就自己去完善了,以下是所有的源代码:
(文件下载出于安全考虑是按流的方式来进行的,而不是直接给出文件下载路径地址,所以像迅雷等下载工具是不能下载的)
首先当然是上传下载的页面了,upfile.jsp
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>File Upload</title>
<script type="text/javascript">
function addFile()
{
var upFile = '<input type="file" name="file1"><br>';
document .getElementById ("files").insertAdjacentHTML("beforeEnd",upFile);
}
function deleteFile()
{
var file = document .getElementById ("files").lastChild;
if(file == null)
return;
document .getElementById ("files").removeChild(file);
file = document .getElementById ("files").lastChild; //移除换行符<br>所以要移两次
document .getElementById ("files").removeChild(file); //如果在表格里面不加<br>就自动换行的,可以去掉,自己把握
}
</script>
</head>
<body>
<h3>基于jspsmart upload组件的文件上传下载</h3>
<form action="servlet/ServletUpload" method="post" enctype="multipart/form-data">
选择文件:<div id="files"><input type="file" name="file1"><br></div>
<input type="submit" value="上传">
<input type="button" value="增加文件" onclick="addFile()">
<input type="button" value="删除文件" onclick="deleteFile()">
<input type="reset" value="重置">
</form>
<br>
<form action="servlet/ServletDownload" method="post">
下载文件的名称:
<input type="text" name="downloadFileName" size="20" maxlength="80">
<input type="submit" value="下载">
</form>
</body>
</html>
然后是实现上传和下载的两个servlet类
上传文件ServletUpload类:
package servlet;
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;
import com.jspsmart.upload.SmartUploadException;
public class ServletUpload extends HttpServlet {
private ServletConfig config;
public ServletUpload() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int count = 0; //记录文件上传总个数
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(config,request, response);
try {
//mySmartUpload.setAllowedFilesList("rar,htm,html,jar");//设置允许上传的文件
mySmartUpload.setDeniedFilesList("exe,jsp,asp");//禁止上传的文件
mySmartUpload.setDenyPhysicalPath(true); //拒绝物理路径
mySmartUpload.setMaxFileSize(5000000);//设置允许上传文件最大为50000bytes
mySmartUpload.setTotalMaxFileSize(50000000);//一次上传文件大小最多不超过5000000bytes
mySmartUpload.upload();
for(int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
String fileName = myFile.getFileName();
System.out.println("文件名:"+fileName);
}
count = mySmartUpload.save("/upload");
System.out.println(count+"文件已上传");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
下载文件ServletDownload类:
package servlet;
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;
import com.jspsmart.upload.SmartUploadException;
public class ServletDownload extends HttpServlet {
private ServletConfig config;
public ServletDownload() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String temp_fileName = request.getParameter("downloadFileName");
if(temp_fileName == null || temp_fileName == "")
return ;
byte[] temp_t = temp_fileName.getBytes("ISO8859_1");
String fileName = new String(temp_t, "GBK");
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(config, request, response);
mySmartUpload.setContentDisposition(null);
/*
* 原型:public void setContentDisposition(String contentDisposition)
* 其中,contentDisposition为要添加的数据。
* 如果contentDisposition为null,则组件将自动添加"attachment;",
* 以表明将下载的文件作为附件,结果是IE浏览器将会提示另存文件,而不是自动打开这个文件
* (IE浏览器一般根据下载的文件扩展名决定执行什么操作,扩展名为doc的将用word程序打开,
* 扩展名为pdf的将用acrobat程序打开,等等)。
*/
try {
mySmartUpload.downloadFile("/upload/"+fileName);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
以下是web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletUpload</servlet-name>
<servlet-class>servlet.ServletUpload</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletDownload</servlet-name>
<servlet-class>servlet.ServletDownload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletUpload</servlet-name>
<url-pattern>/servlet/ServletUpload</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletDownload</servlet-name>
<url-pattern>/servlet/ServletDownload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
要实现无刷新上传其实很简单。只要利用iframe即可,只要把target设置为隐藏的iframe将就可以了。
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>