fileUpload上传文件的方法

本文介绍了一个基于Java的文件上传示例,使用了Apache Commons FileUpload库。通过具体代码展示了如何设置文件大小限制、临时文件存储路径及如何解析HTTP请求中的文件。

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

我用的fileUpload的1.0版本
最新版本没有使用
自己配置web.xml调用
只实现了一些基本的操作~~~

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;

public class Upload extends HttpServlet {

    private String uploadPath = "C://upload//"; // 上传文件的目录
    private String tempPath = "C://upload//tmp//"; // 临时文件目录

    //在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:
    public void doPost(HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException
{
        try {
            DiskFileUpload fu = new DiskFileUpload();
            // 设置最大文件尺寸,这里是4MB
            fu.setSizeMax(4194304);
            // 设置缓冲区大小,这里是4kb
            fu.setSizeThreshold(4096);
            // 设置临时目录:
            fu.setRepositoryPath(tempPath);
   
            // 得到所有的文件:
            List fileItems = fu.parseRequest(request);
            Iterator i = fileItems.iterator();
            // 依次处理每一个文件:
            while(i.hasNext()) {
                FileItem fi = (FileItem)i.next();
                // 获得文件名,这个文件名包括路径:
                    if(fi.getName()!=null){
                        System.out.print("=============Test===============<br/>");
                        String fileName = fi.getName();
                        System.out.print("The file Name is :"+fileName+"<br/>");
                        System.out.print("The content type is :"+fi.getContentType()+"<br/>");
                        System.out.print("The fieldName is "+fi.getFieldName()+"<br/>");
                        System.out.print("The file Size is "+fi.getSize()+"<br/>");
                        //out.print("The file string is "+fi.getString()+"<br/>");  //显示文件按得内容
                       
                         //获得文件名后缀
                        int index = fileName.lastIndexOf('.');
                        String suffix = null;
                        if(index != -1)
                        {
                            suffix = fileName.substring(index + 1, fileName.length());
                        }
                        System.out.print("the suffix is :"+suffix+"<br/>");
                        //生成新文件名
                         String newFileName = /*uploadPath +*/ String.valueOf((new java.util.Date()).getTime())+"."+suffix;
                         System.out.print("the new file name is :"+newFileName+"<br/>");
                        // 在这里可以记录用户和文件信息
                        // ...
                        // 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
                        if(fi.getSize()>500000){
                            System.out.print("文件太大,上传失败!<br/>");
                        }else{
                            fi.write(new File(uploadPath + newFileName));//"a.txt"));
                        }
                }
            }
        }
        catch(Exception e) {
            // 可以跳转出错页面
        }
    }
    //如果要在配置文件中读取指定的上传文件夹,可以在init()方法中执行:
    public void init() throws ServletException {
        uploadPath = "d://upload//";
        tempPath = "d://upload//tmp//";
        // 文件夹不存在就自动创建:
        if(!new File(uploadPath).isDirectory())
            new File(uploadPath).mkdirs();
        if(!new File(tempPath).isDirectory())
            new File(tempPath).mkdirs();
    }
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值