栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Apache commons fileupload文件上传实例讲解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apache commons fileupload文件上传实例讲解

文件上传的方法主要目前有两个常用的,一个是SmartUpload,一个是Apache的Commons fileupload.

我们这里主要介绍下第二个的用法,首先要上传文件,注意几个问题:

  1 form表单内,要添加空间

  2 form表单的内容格式要定义成multipart/form-data格式

  3 需要类库:1 commons-io.jar 2commons-fileupload-1.3.1.jar

接下来我们看下用法。

首先阅读Apache commons fileupload的官方文档可以发现下面几个常用的函数:

1 创建文件解析对象

复制代码 代码如下:DiskFileUpload diskFileUpload = new DiskFileUpload();

2 进行文件解析后放在List中,因为这个类库支持多个文件上传,因此把结果会存在List中。

复制代码 代码如下:List list = diskFileUpload.parseRequest(request);

3 获取上传文件,进行分析(不是必须)

复制代码 代码如下:File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));

4 创建新对象,进行流拷贝

file1 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
     file1.getParentFile().mkdirs();
     file1.createNewFile();
     
     InputStream ins = fileItem.getInputStream();
     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()+"
"); }finally{ ous.close(); ins.close(); }

这样我们就完成了文件的上传。

fileUpload.html

 

web.xml


  UploadServlet
  com.test.hello.UploadServlet
 

  UploadServlet
  /servlet/UploadServlet
 

UploadServlet.java

package com.test.hello;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;

public class UploadServlet extends HttpServlet {

  
  public UploadServlet() {
    super();
  }

  
  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方式上传文件");
  }

  
  @SuppressWarnings({ "unchecked", "deprecation" })
  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();
    try{
      List list = diskFileUpload.parseRequest(request);
      
      out.println("遍历所有的FileItem...
"); for(FileItem fileItem : list){ if(fileItem.isFormField()){ if("description1".equals(fileItem.getFieldName())){ out.println("遍历到description1 ...
"); description1 = new String(fileItem.getString().getBytes(),"UTF-8"); } if("description2".equals(fileItem.getFieldName())){ out.println("遍历到description2 ...
"); 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...
"); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"
"); file1 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName()); file1.getParentFile().mkdirs(); file1.createNewFile(); InputStream ins = fileItem.getInputStream(); 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()+"
"); }finally{ ous.close(); ins.close(); } } if("file2".equals(fileItem.getFieldName())){ File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); out.println("遍历到file2...
"); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"
"); file2 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName()); file2.getParentFile().mkdirs(); file2.createNewFile(); InputStream ins = fileItem.getInputStream(); 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()+"
"); }finally{ ous.close(); ins.close(); } } } out.println("Request 解析完毕

"); } }catch(FileUploadException e){} out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); if(file1 != null){ out.println(""); out.println(" file1;"); out.println(" "+file1.getName()+""); out.println(""); out.println(""); } if(file2 != null){ out.println(""); out.println(" file2;"); out.println(" "+file2.getName()+""); out.println(""); out.println(""); } out.println(""); out.println(" description1:"); out.println(" "); out.println(description1); out.println(""); out.println(""); out.println(""); out.println(" description2:"); out.println(" "); out.println(description2); out.println(""); out.println(""); out.println(" "); out.println(""); out.flush(); out.close(); } public void init() throws ServletException { // Put your code here } }

运行示例

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148949.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号