文件的上传-servlet实现文件上传---核心API—DiskFileItemFactory

本文详细介绍Web开发中的文件上传功能实现,包括如何在Web页面添加上传输入项,如何使用Commons-fileupload组件处理文件上传,以及如何保存文件到本地硬盘等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 文件上传概述

l 实现web开发中的文件上传功能,需完成如下二步操作:

• 在web页面中添加上传输入项

• 在servlet中读取上传文件的数据,并保存到本地硬盘中。

l 如何在web页面中添加上传输入项?

• 标签用于在web页面中添加文件上传输入项,设置文件上传输入项时须注意:

• 1、必须要设置input输入项的name属性,否则浏览器将不会发送上传文件的数据。

• 2、必须把form的enctype属值设为multipart/form-data.设置该值后,浏览器在上传文件时,将把文件数据附带在http请求消息体中,并使用MIME协议对上传的文件进行描述,以方便接收方对上传数据进行解析和处理。

l 如何在Servlet中读取文件上传数据,并保存到本地硬盘中?

• Request对象提供了一个getInputStream方法,通过这个方法可以读取到客户端提交过来的数据。但由于用户可能会同时上传多个文件,在servlet端编程直接读取上传数据,并分别解析出相应的文件数据是一项非常麻烦的工作,示例。

• 为方便用户处理文件上传数据,Apache 开源组织提供了一个用来处理表单文件上传的一个开源组件(Commons-fileupload ),该组件性能优异,并且其API使用极其简单,可以让开发人员轻松实现web文件上传功能,因此在web开发中实现文件上传功能,通常使用Commons-fileupload组件实现。

l 使用Commons-fileupload组件实现文件上传,需要导入该组件相应的支撑jar包:Commons-fileupload和commons-io。commons-io 不属于文件上传组件的开发jar文件,但Commons-fileupload 组件从1.1 版本开始,它工作时需要commons-io包的支持。

二 核心API—DiskFileItemFactory

l DiskFileItemFactory 是创建FileItem 对象的工厂,这个工厂类常用方法:

• public void setSizeThreshold(int sizeThreshold) :设置内存缓冲区的大小,默认值为10K。当上传文件大于缓冲区大小时,fileupload组件将使用临时文件缓存上传文件。

• public void setRepository(java.io.File repository) :指定临时文件目录,默认值为System.getProperty(“java.io.tmpdir”).

• public DiskFileItemFactory(int sizeThreshold,java.io.File repository) :构造函数

l ServletFileUpload 负责处理上传的文件数据,并将表单中每个输入项封装成一个FileItem 对象中。常用方法有:

• boolean isMultipartContent(HttpServletRequest request) :判断上传表单是否为multipart/form-data类型

• List parseRequest(HttpServletRequest request):解析request对象,并把表单中的每一个输入项包装成一个fileItem 对象,并返回一个保存了所有FileItem的list集合。

• setFileSizeMax(long fileSizeMax) :设置上传文件的最大值

• setSizeMax(long sizeMax) :设置上传文件总量的最大值

• setHeaderEncoding(java.lang.String encoding) :设置编码格式

• setProgressListener(ProgressListener pListener)

三 文件上传案例

l 实现步骤

1、创建DiskFileItemFactory对象,设置缓冲区大小和临时文件目录

2、使用DiskFileItemFactory 对象创建ServletFileUpload对象,并设置上传文件的大小限制。

3、调用ServletFileUpload.parseRequest方法解析request对象,得到一个保存了所有上传内容的List对象。

4、对list进行迭代,每迭代一个FileItem对象,调用其isFormField方法判断是否是上传文件

• 为普通表单字段,则调用getFieldName、getString方法得到字段名和字段值

• 为上传文件,则调用getInputStream方法得到数据输入流,从而读取上传数据。

l 编码实现文件上传

package com.hbsi.servlet;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.apache.commons.fileupload.FileItem;

importorg.apache.commons.fileupload.FileUploadException;

importorg.apache.commons.fileupload.disk.DiskFileItemFactory;

importorg.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet2 extendsHttpServlet {

     /**

      * The doGet method of the servlet. <br>

      *

      * This method is called when a form has itstag value method equals to get.

      *

      * @param request the request send by theclient to the server

      * @param response the response send by theserver to the client

      * @throws ServletException if an erroroccurred

      * @throws IOException if an error occurred

      */

     publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

                        throwsServletException, IOException {



               doPost(request,response);

     }



     /**

      * The doPost method of the servlet. <br>

      *

      * This method is called when a form has itstag value method equals to post.

      *

      * @param request the request send by theclient to the server

      * @param response the response send by theserver to the client

      * @throws ServletException if an erroroccurred

      * @throws IOException if an error occurred

      */

     publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

                        throwsServletException, IOException {

               response.setContentType("text/html;charset=utf-8");

               //文件上传

               try{

               //创建一个解析器工厂

               DiskFileItemFactoryfactory=new  DiskFileItemFactory();

               //得到解析器

               ServletFileUploadupload=new ServletFileUpload(factory);

               upload.setHeaderEncoding("utf-8");

               //if(upload.isMultipartContent(request))

               //对相应的请求进行解析,有几个输入项,就有几个FileItem对象

    

                        List<FileItem>list=upload.parseRequest(request);

                        //要迭代list集合,获取list集合中每一个输入项

                        for(FileItemitem:list){

                                 //判断输入的类型

                                 if(item.isFormField()){

                                           //普通输入项

                                           StringinputName=item.getFieldName();

                                           StringinputValue=item.getString();

                                           System.out.println(inputName+"::"+inputValue);

                                 }else{

                                           //上传文件输入项

                                           Stringfilename=item.getName();//上传文件的文件名1.txt

                                           filename=filename.substring(filename.lastIndexOf("\\")+1);

                                           InputStreamis=item.getInputStream();

                                           FileOutputStreamfos=new FileOutputStream("c:\\"+filename);

                                           byte[]buff=new byte[1024];

                                           intlen=0;

                                           while((len=is.read(buff))>0){

                                                    fos.write(buff);

                                           }

                                           is.close();

                                           fos.close();

                                 }

                                

                        }

                        request.setAttribute("message","okokokokokoko");

               }catch (FileUploadException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                        request.setAttribute("message","nonononononono");

               }

              

               request.getRequestDispatcher("/message.jsp").forward(request,response);



              

     }

-----------start------------

//web文件上传

public Object UploadForWeb(HttpServletRequest request){
File f = null;
// 创建 DiskFileItemFactory 对象
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);// 超过1m的数据采用临时文件缓存
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(1024 * 1024 * 1024);// 单个文件大小
upload.setHeaderEncoding(ProtocolConstant.PROTOCAL_UTF8_ENCODE);// 设置普通字段名称和文件字段的文件名所采用的字符集编码
upload.setSizeMax(1024 * 1024 * 1024);// 最多上传2m的文件
List list = null;
try
{
list = upload.parseRequest(httpRequest);
}
catch (FileUploadException e)
{
log.error(e);
throw new PortalException(PortalExceptionConstant.PARSE_FILE_STREAM, “解析需上传文件异常”);
}
String dirType = getDirTypeFromList(list);
if (StringTools.isEmpty(dirType))
{
throw new Exception(ExceptionConstant.EMPTY_PARAM_ERR_NO, "empty param is "
+ FileConfigConstant.FILE_CATALOG_CONFIG);
}

    Calendar ca = Calendar.getInstance();
    StringBuffer currentDay = new StringBuffer().append(ca.get(Calendar.YEAR))
        .append(FILE_SPLIT)
        .append((ca.get(Calendar.MONTH) + 1))
        .append(FILE_SPLIT)
        .append(ca.get(Calendar.DATE))
        .append(FILE_SPLIT);
    UPLOAD_PATH.append(currentDay);
    f = new File(UPLOAD_PATH.toString().replace(DIR_TYPE_SIGN, dirType));
    if (!f.exists())
    {
        if (!f.mkdirs())
        {
            throw new Exception(ExceptionConstant.FILE_OPERATE_ERR_NO, "创建文件失败:" + f.getAbsolutePath());
        }

       //调用jdk中的调用shell命令给文件赋权限
        chmod(chmodCmd, f);
    }
    String rela_path = RELATIVE_PATH.append(currentDay).toString().replace(DIR_TYPE_SIGN, dirType);
    FileUploadResponse response = new FileUploadResponse();
    Map<String, String> prop = new HashMap<String, String>();
    List<String> failFiles = new ArrayList<String>();
    Iterator<FileItem> it = list.iterator(); // 遍历list
    // 解压文件
    UnzipContentHelper unzipHelper = new UnzipContentHelper();
    
    while (it.hasNext())
    {
        FileItem fi = it.next();
        // 如果为文件流
        if (!fi.isFormField())
        {
            // 判断该 FileItem 对象是否是一个普通表单类型
            // try
            // {
            String pathStr = fi.getName();// 得到文件表单的值,就是用户本地的文件路径
            // 如果文件表单为空,则不处理
            if (StringUtils.isEmpty(pathStr.trim()))
            {
                continue;
            }
            // int start = pathStr.lastIndexOf(FILE_SPLIT);// 得到文件名在路径中的位置
            // String fileName = pathStr.substring(start + 1);// 得到文件名
            int index = pathStr.lastIndexOf(".");
            // 生成文件名称
            String generate_file_name = UUID.randomUUID().toString();
            // 如果文件没有后缀,则使用一个单独目录存放
            String file_type;
            if (index <= 0)
            {
                file_type = NO_SUFFIX_FILE_DIR;
            }
            else
            {
                file_type = pathStr.substring(index + 1);
                generate_file_name += "." + file_type;
            }
            File pathDest = new File(f, file_type + FILE_SPLIT + generate_file_name);
            if (!pathDest.exists())
            {
                // 如果上级目录不存在,并创建上级目录失败或者创建文件失败,则保存文件名到错误文件列表中
                try
                {
                    //创建父目录
                    if(!pathDest.getParentFile().exists())
                    {
                        pathDest.getParentFile().mkdirs();
                    }
                    
                    if (!pathDest.getParentFile().exists() || !pathDest.createNewFile())
                    {
                        response.putFailReason("创建文件" + pathDest.getParentFile().getPath() + "失败;");
                        failFiles.add(pathStr);
                        continue;
                    }
                    chmod(chmodCmd, pathDest.getParentFile());
                }
                catch (IOException e)
                {
                    response.putFailReason("创建文件" + pathDest.getPath() + "失败;");
                    failFiles.add(pathStr);
                    continue;
                }
            }
            try
            {
                if(log.isDebugEnabled())
                {
                    log.debug("get file size:" + fi.getSize());
                }
                //apache 公共组件bug,当文件字节小于一定值时(约为30B),可能写入后为空值。
                if(fi.getSize() <= 0)
                {
                    response.putFailReason("文件内容太小导致上传失败,请重新选择文件;");
                    failFiles.add(fi.getName());
                }
                else
                {
                    fi.write(pathDest); // 写文件
                }
                rela_path += file_type + FILE_SPLIT + generate_file_name;
            }
            catch (Exception e)
            {
                response.putFailReason("写入文件" + pathDest.getPath() + "失败;");
                failFiles.add(pathStr);
                continue;
            }
            prop.put(pathStr, BRANCH_NUMBER + FILE_SPLIT + rela_path);
            
            String unzip = getUnzipTypeFromList(list);
            if (!StringTools.isEmpty(unzip) && ZIPTYPE_ZIP.equals(unzip) && "zip".equals(file_type))
            {
                try
                {
                    // fileName: 文件名.后缀名, sourcePath:原文件绝对路径+文件名
                    unzipHelper.unZipAllFile(generate_file_name, pathDest.getPath());
                }
                catch (Exception e)
                {
                    response.putFailReason("解压文件:" + pathDest.getPath() + " 失败;" + " 原因:" + e.getMessage());
                    failFiles.add(pathStr);
                    continue;
                }
            }
        }
    }
    response.setSuccFiles(prop);
    response.setFailFiles(failFiles);
    return response;

}

----end-------

}

博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导):

http://blog.youkuaiyun.com/java958199586/article/details/7003747


http://www.jb51.net/article/35172.htm ----FileUpload的两种上传方式:Traditional API上传方式/Streaming API上传方式


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值