如题,这个上传下载功能用的是jspsmartupload组件,要到网上下载jar包
先上一个简单的案例程序:
上传:
前台:
<%@ page contentType="text/html;charset=UTF-8"%>
jspSmartUpload组件文件上传
jspSmartUpload组件文件上传
后台:
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 SmartUploadServlet extends HttpServlet {
private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
//响应POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取PrintWriter对象
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("A Servlet");
out.println("
");
out.println("
jspSmartUpload
");
out.println("
");
//新建一个SmartUpload对象
SmartUpload mySmartUpload=new SmartUpload();
try{
//上传初始化
mySmartUpload.initialize(config, request, response);
//设定每个上传文件的最大长度
mySmartUpload.setMaxFileSize(1*512*1024);
//设定总上传数据的长度
mySmartUpload.setTotalMaxFileSize(1*1024*1024);
//设定允许上传的文件的类型,只允许上传java,doc,txt文件
mySmartUpload.setAllowedFilesList("java,doc,txt");
//设定禁止上传的文件的类型,禁止上传带有exe,bat文件
mySmartUpload.setDeniedFilesList("exe,bat");
//上传文件
mySmartUpload.upload();
//将上传文件全部保存到指定目录
String name = new String(mySmartUpload.getRequest().getParameter("paramete"));
System.out.println(name);
int count=mySmartUpload.save("/");
out.println(//利用Request对象获取参数之值
//String name= new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") ;
mySmartUpload.getRequest().getParameter("paramete")+" ");
out.println(name);
//显示处理结果
out.println("
"+count+" File Upload!
");
//处理每个上传文件
for(int i=0;i
" +file.getFileName()+"
"); //打印文件扩展名 out.println("File ExtName:
" +file.getFileExt()+"
"); //打印文件路径,包括目录 out.println("Path:
" +file.getFilePathName()+"
"); //另存到以Web应用程序的根目录为文件根目录的目录下 //(声明一下:在Myeclipse中,该目录位于工程下的.metadata/.me_tcat/webapps/该工程目录/upload/) file.saveAs("/upload/"+file.getFileName(),mySmartUpload.SAVE_VIRTUAL); } } }catch(Exception e){//异常处理 //打印自定义异常信息 out.println("Unanable to upload the file.
"); out.println("Please Check The File Type"); } } }
下载:
前台:
<%@ page contentType="text/html;charset=UTF-8"%>
jspSmartUpload组件下载文件
jspSmartUpload组件下载文件
后台:
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 SmartDownloadServlet extends HttpServlet {
private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
//响应POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//新建一个SmartUpload对象
SmartUpload mySmartUpload=new SmartUpload();
try{
//上传初始化
mySmartUpload.initialize(config, request, response);
//String name= new String(request.getParameter("name"