jsp/servlet上传下载实例时间:2022-08-28 09:31:38上传页面: <form action="upload" method="post" enctype="multipart/form-data" >上传文件:<input type="file" name="file" /><input type="submit" name="Submit" value="upload" /></form> 上传servlet: package cn.com.pearl.servlet;import java.io.File;import java.io.IOException;import java.util.Date;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.DiskFileUpload;import org.apache.commons.fileupload.FileItem;import cn.com.pearl.dao.NewsDao;import cn.com.pearl.pojo.Upload;@SuppressWarnings("deprecation")public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 281275200609936729L;String uploadPath = "";private NewsDao newsDao = new NewsDao();String title = null;@SuppressWarnings("unchecked")public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {String upath = "";String uname = "";try {DiskFileUpload fu = new DiskFileUpload();fu.setHeaderEncoding("utf8");// 设置最大文件尺寸,这里是4MBfu.setSizeMax(4194304);// 设置缓冲区大小,这里是4kbfu.setSizeThreshold(4096);// 设置临时目录:// fu.setRepositoryPath(tempPath);// 得到所有的文件:List fileItems = fu.parseRequest(request);Iterator i = fileItems.iterator();// 依次处理每一个文件:while (i.hasNext()) {FileItem fi = (FileItem) i.next();// 获得文件名,这个文件名包括路径:String fileName = fi.getName();if (fileName != null) {if (fileName != null) {fi.write(new File(uploadPath + fileName));upath = "/upload/" + fileName;uname = fileName;Upload ul = new Upload();ul.setUname(uname);ul.setUpath(upath);ul.setCreateTime(new Date());newsDao.addUpload(ul);}}}request.getRequestDispatcher("/uploadManager").forward(request,response);} catch (Exception e) {e.printStackTrace();response.sendRedirect("error.jsp");}}public void init() throws ServletException {// 文件夹不存在就自动创建:uploadPath = this.getServletContext().getRealPath("/") + "/upload/";if (!new File(uploadPath).isDirectory()) {new File(uploadPath).mkdirs();System.out.println("mkdirs success...");}}} 下载页面: <a href="down?fileName=文件.pdf " mce_href="down?fileName=文件.pdf " >文件.pdf</a> 下载Servlet package cn.com.pearl.servlet;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownServlet extends HttpServlet {private static final long serialVersionUID = 8594448765428224944L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/* * 处理请求 (non-Javadoc) * * @see * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest * , javax.servlet.http.HttpServletResponse) */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//接收中文参数乱码处理response.setCharacterEncoding("utf-8");String name = new String(request.getParameter("fileName").getBytes("ISO-8859-1"), "utf-8");//得到文件名name = name.substring(8);// web绝对路径String path = request.getSession().getServletContext().getRealPath("/");String savePath = path + "upload";// 设置为下载application/x-downloadresponse.setContentType("application/x-download");// 即将下载的文件在服务器上的绝对路径String filenamedownload = savePath + "/" + name;// 下载文件时显示的文件保存名称String filenamedisplay = name;// 中文编码转换filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");response.addHeader("Content-Disposition", "attachment;filename="+ filenamedisplay);try {java.io.OutputStream os = response.getOutputStream();java.io.FileInputStream fis = new java.io.FileInputStream(filenamedownload);byte[] b = new byte[1024];int i = 0;while ((i = fis.read(b)) > 0) {os.write(b, 0, i);}fis.close();os.flush();os.close();} catch (Exception e) {}}}