基于apache commons-fileupload-1.2组件的文件上传

apache commons-fileupload-1.2 组件下载地址: http://jakarta.apache.org/commons/fileupload/,同时该组件需要Commons IO的支持,下载地址: http://jakarta.apache.org/commons/io/

--------------------------------------------------------------------
用来处理上传文件的Upload.java(Servlet )
--------------------------------------------------------------------
package common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;

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

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

public final class Upload extends HttpServlet {
   
    private final String savepath = new String ("E://upload//");
   
    public Upload() {
        super();
    }
   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws IOException, ServletException{
       try {
           request.setCharacterEncoding("gb2312");
          
           if(ServletFileUpload.isMultipartContent(request)){
               ServletFileUpload upload = new ServletFileUpload();
               FileItemIterator iter = upload.getItemIterator(request);
              
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    String name = item.getFieldName();
                    InputStream stream = item.openStream();
                   
                    if (item.isFormField()) {
                        System.out.println("Form field " + name + " with value "
                            + Streams.asString(stream) + " detected.");
                    } else {
                        String filename = new File(item.getName()).getName();
                        System.out.println("File field " + name + " with file name "
                            + filename + " detected.");
                        BufferedInputStream bis = new BufferedInputStream(stream);
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(savepath + filename)));
                        Streams.copy(bis, bos, true);
                    }
                }
               
                response.getWriter().write("<p>succeed</p>");
            }
       }
       catch(Exception e) {
           e.printStackTrace();
       }
    }
}
--------------------------------------------------------------------
测试用的HTML网页
--------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
    <title>upload Test</title>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
  <label>Test
  <input type="text" name="text1">
  </label>
  <br>
  <label>File
      <input type="file" name="file">
  </label>
  <input type="submit" name="Submit" value="upload">
</form>
</body>
</html>
--------------------------------------------------------------------
配置web.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Upload</servlet-name>
    <servlet-class>common.Upload</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Upload</servlet-name>
    <url-pattern>/upload.html</url-pattern>
  </servlet-mapping>

</web-app>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值