当前位置:编程学习 > 网站相关 >>

使用common-fileupload实现文件的上传

文件上传是网站非常常用的功能,直接使用Servlet获取上传文件还得解析请求参数,比较麻烦,所以一般选择采用

apache的开源工具,common-fileupload.这个jar包可以再apache官网上面找到,也可以在struts的lib文件夹下面找到,

struts上传的功能就是基于这个实现的。

common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。然后导入到你的项目路径下面。

使用代码如下

[java]
package oop.hg.ytu.servlet; 
 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.List; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import oop.hu.ytu.dao.UploadDomain; 
 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
 
public class Upload extends HttpServlet { 
 
    /**
     * 处理用户上传请求
     */ 
    private static final long serialVersionUID = 1L; 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
    //  String describe = request.getParameter("describe");  
        DiskFileItemFactory factory = new DiskFileItemFactory(); 
        @SuppressWarnings("deprecation") 
        String path = request.getRealPath("/upload");//设置磁盘缓冲路径  
     
        factory.setRepository(new File(path)); 
        factory.setSizeThreshold(1024*1024);//设置创建缓冲大小  
         
        ServletFileUpload upload = new ServletFileUpload(factory); 
        upload.setSizeMax(-1);//设置上传文件限制大小,-1无上限  
        try { 
            @SuppressWarnings("unchecked") 
            List<FileItem> list = upload.parseRequest(request); 
            String va = null; 
            for(FileItem item : list){ 
        //      String name = item.getFieldName();  
                if(item.isFormField()){//判断是否是文件流  
                     
                    va = item.getString("UTF-8"); 
                //  System.out.println(name+"="+va);  
            ///     request.setAttribute(name, value);  
                }else{ 
                    String value = item.getName();//会将完整路径名传过来  
                    int start = value.lastIndexOf("\\"); 
                    String fileName = value.substring(start+1); 
            //      request.setAttribute(name, fileName);  
                    InputStream in = item.getInputStream(); 
                    UploadDomain dao = new UploadDomain(); 
                    //item.write(new File(realPath,fileName));  
                    int index = fileName.lastIndexOf("."); 
                    String realFileName = fileName.substring(0,index); 
                    String type = fileName.substring(index+1); 
                    dao.insert(in, realFileName,type,va);//放入到数据库中  
                     
                } 
            } 
        } catch (Exception e) { 
             
            e.printStackTrace(); 
        } 
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doGet(request, response); 
 
    } 
 

package oop.hg.ytu.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oop.hu.ytu.dao.UploadDomain;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class Upload extends HttpServlet {

 /**
  * 处理用户上传请求
  */
 private static final long serialVersionUID = 1L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   th

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,