欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp+servlet实现文件上传与下载功能

程序员文章站 2022-04-28 23:06:31
本文实例为大家分享了jsp servlet实现文件上传与下载的具体代码,供大家参考,具体内容如下 上传: 需要导入两个包:commons-fileupload-1....

本文实例为大家分享了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<fileitem> 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) {

 }

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。