jsp+servlet实现文件上传与下载功能
本文实例为大家分享了jsp servlet实现文件上传与下载的具体代码,供大家参考,具体内容如下
上传:
需要导入两个包:commons-fileupload-1.2.1.jar,commons-io-1.4.jar
import java.io.file; import java.io.ioexception; 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.fileitem; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; /** * 上传附件 * @author new * */ public class uploadannexservlet extends httpservlet { private static string path = ""; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } /* * post处理 * (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 { path = this.getservletcontext().getrealpath("/upload"); try { diskfileitemfactory factory = new diskfileitemfactory(); servletfileupload up = new servletfileupload(factory); list ls = up.parserequest(request); for (fileitem fileitem : ls) { if (fileitem.isformfield()) { string fieldname = fileitem.getfieldname(); //getname()返回的是文件名字 普通域没有文件 返回null // string name = fileitem.getname(); string content = fileitem.getstring("gbk"); request.setattribute(fieldname, content); } else { string nm = fileitem.getname().substring( fileitem.getname().lastindexof("\\") + 1); file mkr = new file(path, nm); if (mkr.createnewfile()) { fileitem.write(mkr);//非常方便的方法 } request.setattribute("result", "上传文件成功!"); } } } catch (exception e) { e.printstacktrace(); request.setattribute("result", "上传失败,请查找原因,重新再试!"); } request.getrequestdispatcher("/pages/admin/annex-manager.jsp").forward( request, response); } }
下载(i/o流)无需导包:
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; /** * 下载文件 * @author * */ public class downloadfilesservlet 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 { string name = request.getparameter("filename"); system.out.print("dddddddddd:" + name); // web绝对路径 string path = request.getsession().getservletcontext().getrealpath("/"); string savepath = path + "upload"; // 设置为下载application/x-download response.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) { } } }
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法