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

JavaWeb文件上传开发实例

程序员文章站 2024-03-13 12:57:45
文件上传有smartupload、apache的commons fileupload.我们今天介绍commons fileupload的用法。  1.com...

文件上传有smartupload、apache的commons fileupload.我们今天介绍commons fileupload的用法。 

1.commons-fileupload-1.3.1.jar
下载地址:

2.web.xml

<!-- 上传文件 服务器端 -->
 <servlet>
 <servlet-name>uploadservlet</servlet-name>
 <servlet-class>com.servlet.uploadservlet</servlet-class>
 <init-param>
 <param-name>filepath</param-name><!-- 文件存储路径 -->
 <param-value>f:\myeclipse\workspaces\king\webroot\uploadfile\</param-value>
 </init-param>
 </servlet>
 
 <servlet-mapping>
 <servlet-name>uploadservlet</servlet-name>
 <url-pattern>/servlet/uploadservlet</url-pattern>
 </servlet-mapping>
3.uploadfile.jsp
<form action="servlet/uploadservlet" method="post" enctype="multipart/form-data">
  <div align="center">
   <fieldset style="width:80%">
    <legend>上传文件</legend><br/>
     <div align="left">上传文件1</div>
     <div align="left">
      <input type="file" name="file1"/>
     </div>
     <div align="left">上传文件2</div>
     <div align="left">
      <input type="file" name="file2"/>
     </div>
     <div>
      <div align='left'>上传文件说明1</div>
      <div align='left'><input type="text" name="description1"/></div>
     </div>
     <div>
      <div align='left'>上传文件说明2</div>
      <div align='left'><input type="text" name="description2"/></div>
     </div>
     <div>
      <div align='left'>
       <input type='submit' value="上传文件"/>
      </div>
     </div>
   </fieldset>
  </div>
 </form>

3.servlet

package com.servlet;

import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.printwriter;
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 org.apache.commons.fileupload.fileuploadexception;

import sun.swing.filepane;

public class uploadservlet extends httpservlet {

 private string filepath;


 /**
  * destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // just puts "destroy" string in log
  // put your code here
 }

 
 public void doget(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {

  response.setcharacterencoding("utf-8");
  response.getwriter().println("请以post方式上传文件");
 }

 
 public void dopost(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  file file1 = null, file2 = null;
  string description1 = null, description2 = null;
  response.setcharacterencoding("utf-8");
  request.setcharacterencoding("utf-8");
  response.setcontenttype("text/html");
  printwriter out = response.getwriter();
  
  diskfileupload diskfileupload = new diskfileupload();//解析request
  
  try {
   list<fileitem> filelist = diskfileupload.parserequest(request);//将结果放置到list中
   out.println("遍历所有的fileitem...</br>");
   this.log("遍历所有的fileitem...");
   for(fileitem fileitem : filelist){
    if(fileitem.isformfield()){//如果是文本域
     if("description1".equals(fileitem.getfieldname())){
      out.println("遍历到description1...</br>");
      description1 = new string(fileitem.getstring().getbytes(),"utf-8");
     }
     if("description2".equals(fileitem.getfieldname())){
      out.println("遍历到description2...</br>");
      description2 = new string(fileitem.getstring().getbytes(),"utf-8");
     }
    }else{//否则为文件域
     if("file1".equals(fileitem.getfieldname())){
      file remotefile = new file(new string(fileitem.getname().getbytes(),"utf-8"));
      out.println("遍历到file1...</br>");
      out.println("客户端文件位置:"+remotefile.getabsolutepath()+"</br>");
      //服务器端文件,放在uploadfile文件夹下
      file1 = new file(filepath,remotefile.getname());
      file1.getparentfile().mkdirs();//创建文件夹路径
      file1.createnewfile();//创建新文件
      inputstream ins = fileitem.getinputstream();//fileitem的内容
      outputstream ous = new fileoutputstream(file1);//输出到文件中
      try{
       byte[] buffer = new byte[1024];//字节缓存
       int len = 0;//实际缓存长度
       while((len=ins.read(buffer)) > -1){//循环读入缓存
        ous.write(buffer,0,len);
       }
       out.println("已保存文件"+file1.getabsolutepath()+"</br>");
      }
      finally{
       ous.close();
       ins.close();
      }
     }
     if("file2".equals(fileitem.getfieldname())){
      file remotefile = new file(new string(fileitem.getname().getbytes(),"utf-8"));
      out.println("遍历到file2...</br>");
      out.println("客户端文件位置:"+remotefile.getabsolutepath()+"</br>");
      //服务器端文件,放在uploadfile文件夹下
      file2 = new file(filepath,remotefile.getname());
      file2.getparentfile().mkdirs();//创建文件夹路径
      file2.createnewfile();//创建新文件
      inputstream ins = fileitem.getinputstream();//fileitem的内容
      outputstream ous = new fileoutputstream(file2);//输出到文件中
      try{
       byte[] buffer = new byte[1024];//字节缓存
       int len = 0;//实际缓存长度
       while((len=ins.read(buffer)) > -1){//循环读入缓存
        ous.write(buffer,0,len);
       }
       out.println("已保存文件"+file2.getabsolutepath()+"</br>");
      }
      finally{
       ous.close();
       ins.close();
      }
     }
    }
   }
   out.println("request 解析完毕");
  } catch (fileuploadexception e) {
   // todo 自动生成的 catch 块
   out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">");
   out.println("<html>");
   out.println(" <head><title>a servlet</title></head>");
   out.println(" <body>");
   
   if(file1 != null){
    out.println("<div>");
    out.println(" <div align='left'>file1;</div>");
    out.println(" <div align='left'><a href='"+request.getcontextpath()+
      file1.getname()+"'target=_blank>"+file1.getname()+"</a>");
    out.println("</div>");
    out.println("</div>");
   }
   if(file2 != null){
    out.println("<div>");
    out.println(" <div align='left'>file2;</div>");
    out.println(" <div align='left'><a href='"+request.getcontextpath()+
      file2.getname()+"'target=_blank>"+file2.getname()+"</a>");
    out.println("</div>");
    out.println("</div>");
   }
   out.println("<div>");
   out.println(" <div align='left'>description1:</div>");
   out.println(" <div align='left'>");
   out.println(description1);
   out.println("</div>");
   out.println("</div>");
   
   out.println("<div>");
   out.println(" <div align='left'>description2:</div>");
   out.println(" <div align='left'>");
   out.println(description2);
   out.println("</div>");
   out.println("</div>");
   
   out.println(" </body>");
   out.println("</html>");
   out.flush();
   out.close();
  }
  
 }

 public string getservletinfo() {
  return "this is my default servlet created by eclipse";
 }

 
 public void init() throws servletexception {
  // put your code here
  filepath = getservletconfig().getinitparameter("filepath");
 }

}

JavaWeb文件上传开发实例JavaWeb文件上传开发实例

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