fileupload

转载 2012年03月29日 20:31:08

lib:commons-fileupload-1.1.jar;commons-io-1.2.jar

静态页面文件代码如下


<HTML>
<BODY>
<form name="form1" method="post" action="c4.jsp" enctype="multipart/form-data">

<table width="85%" border="0">
  <tr>
    <td>
        名字<input type="text" name="username" size="40">
    </td>
  </tr>

  <tr>
    <td>
     图片   <input type="file" name="imagefile" size="40">
    </td>
  </tr>

  <tr>
    <td>
      word  <input type="file" name="wordfile" size="40">
    </td>
  </tr>

  <tr>
    <td>复选框 
        <input type="checkbox" name="software" value="struts">
        Struts
        <input type="checkbox" name="software" value="tomcat">
        Tomcat
    </td>
  </tr>

  <tr>
    <td align="left">单选框:
      <input type="radio" name="team" value="indians">
      Indians
      <input type="radio" name="team" value="yankees">
      Yankees
    </td>
  </tr>

  <tr>
    <td>多选下拉:
      <select name="books" size="5" multiple>
        <option value="ajp">Advanced JavaServer Pages</option>
        <option value="sia">Struts In Action</option>
      </select>
    </td>
  </tr>

  <tr>
    <td colspan="2" align="center">
      <input type="submit" name="submit" value="提交">
    </td>
  </tr>
</table>
</form>
</BODY>
</HTML>

------------------------

c4.jsp的内容如下

<%@page import="practice.FileUpLoader2"%>
<%
 FileUpLoader2 fileUpload = new FileUpLoader2(request);
     fileUpload.upload(request);
request.getRequestDispatcher("msg.jsp").forward(request, response);
%>

------------------------

msg.jsp内容如下

<%@page language="java" contentType="text/html;charset=GBK"%>
<%if (request.getAttribute("message") != null){%>
<div><%=request.getAttribute("message")%><%request.removeAttribute("message")%}>  <div>

-----------------------

FileUpLoader2内容如下

package practice;


import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItem;


public class FileUpLoader2 {
    //文件上传出错后在页面提示的消息
    static StringBuffer message = new StringBuffer();
    //当上传文件超过限制时设定的临时文件位置
    private String tempPath = "d:\\tempzjd";

    //文件上传目标目录
    private String destinationPath = "d:\\tempzjd2";

    //获取的上传请求
    private HttpServletRequest fileuploadRequest = null;

    //设置最多只允许在内存中存储的数据,单位:字节
    private int sizeThreshold = 4096;

    //设置允许用户上传文件大小,单位:字节
    //共100M
    private long sizeMax = 104857600;

    public FileUpLoader2() {
    }


    public FileUpLoader2(HttpServletRequest fileuploadRequest) {
        this.fileuploadRequest = fileuploadRequest;
    }


    public void upload(HttpServletRequest request) throws FileUploadException {
        //如果请求不是文件上传,则返回
        if (!FileUpload.isMultipartContent(request)) {
            message.append("Request is not multipart!");
            request.setAttribute("message", message.toString());
            return;
        }
        //如果没有临时目录,则创建它
        if (!(new File(tempPath).isDirectory())) {
            try {
                new File(tempPath).mkdirs();
            } catch (SecurityException e) {
                System.out.println("can not make security direcoty");
            }
        }

        //如果没有上传目的目录,则创建它
        if (!(new File(destinationPath).isDirectory())) {
            try {
                new File(destinationPath).mkdirs();
            } catch (SecurityException e) {
                System.out.println("can not make security direcoty");
            }
        }

        DiskFileUpload fileUpload = new DiskFileUpload(new
                DefaultFileItemFactory());
//        DiskFileItemFactory factory = new DiskFileItemFactory();
        fileUpload.setSizeThreshold(sizeThreshold);
        fileUpload.setSizeMax(sizeMax);
        fileUpload.setRepositoryPath(tempPath);

        List items = fileUpload.parseRequest(request);
        Iterator itr = items.iterator();
        FileItem item;
        while (itr.hasNext()) {
            item = (FileItem) itr.next();

            String fieldName = item.getFieldName();
            if (item.isFormField()) { //对普通的form内容处理
                String fieldValue = item.getString();
                if (fieldName.equals("software")) {
                    System.out.println(fieldValue);
                } else {
                    System.out.println(fieldValue);
                }
            } else {
                if (item.getSize() <= 0) {
                    continue;
                }
                String contentType = item.getContentType();
                File uploadedFile = new File(item.getName());
                File savedFile = null;
                if (fieldName.equals("imagefile")) { //imagefile对应页面上input的名字
                    if (!contentType.startsWith("image")) {
                        message.append("Incorrect file uploaded! " +
                                       uploadedFile.getName() +
                                       "is not of type image."
                                );
                        continue;
                    }

                    savedFile = new File(destinationPath, uploadedFile.getName());
                    try {
                        item.write(savedFile);
                    } catch (Exception ex) {
                    }
                } else {
                    if (contentType.indexOf("msword") == -1) {
                        message.append("Incorrect File uploaded! " +
                                       uploadedFile.getName() +
                                       " is not of type Word."
                                );
                        continue;
                    }
                    savedFile = new File(destinationPath, uploadedFile.getName());
                    try {
                        item.write(savedFile);
                    } catch (Exception ex1) {
                    }
                }
            }
        }
        request.setAttribute("message", message.toString());
    }

    public String getTempPath() {
        return tempPath;
    }

    public int getSizeThreshold() {
        return sizeThreshold;
    }

    public long getSizeMax() {
        return sizeMax;
    }


    public HttpServletRequest getFileuploadRequest() {
        return fileuploadRequest;
    }

    public String getDestinationPath() {
        return destinationPath;
    }

    public StringBuffer getMessage() {
        return message;
    }

    public void setDestinationPath(String destinationPath) {
        this.destinationPath = destinationPath;
    }

    public void setFileuploadRequest(HttpServletRequest fileuploadRequest) {
        this.fileuploadRequest = fileuploadRequest;
    }


    public void setSizeMax(long sizeMax) {
        this.sizeMax = sizeMax;
    }

    public void setSizeThreshold(int sizeThreshold) {
        this.sizeThreshold = sizeThreshold;
    }

    public void setTempPath(String tempPath) {
        this.tempPath = tempPath;
    }

    public void setMessage(StringBuffer message) {
        this.message = message;
    }


}
 附录:普通文件的contentType

world: application/msword
excel: application/vnd.ms-excel
pdf: application/pdf
txt: text/plain
ppt: application/vnd.ms-powerpoint
htm: text/html

相关文章推荐

richfaces fileupload组件的使用方法

richfaces fileupload组件是用于文件批量上传的组件,用他做JSF的文件上传是非常方便和高效的.但有些问题需要注意,配置方法如下 1、先修改web.xml配置文件   Ajax...

使用servlet实现文件上传(用common-fileUpload和common-io包)

用servlet实现文件的上传,这里用到Apache的开源工具:common-fileupload。因为common-fileupload依赖于common-io,所以需要同时下载两个包。 co...
  • FateHJ
  • FateHJ
  • 2016年04月03日 15:07
  • 2046

Spring MVC 4 使用常规的fileupload上传文件(带源码)

【本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看。源码下载地址在文章末尾。】 【翻译 by 明明如月 QQ 605283073】 上一篇:  Spri...

fileupload上传文件

jsp页面 用户名: 密 码: 文件1: 文件2: 后台servlet 1 2public class UploadServlet e...
  • lsh6688
  • lsh6688
  • 2013年06月30日 19:50
  • 7665

解决asp.net中Fileupload上传在IE不同版本下显示图片

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> http://www.w3.org/1999/xhtml">     修改设...

《Java》----详解用apache的commons-fileupload组件实现文件上传

在web开发中,文件上传功能是很普遍的,我们最常见的就是上传图片了,还有上传Excel,今天小编给大家分享一下利用apache的组件实现文件上传功能。     文件上传总体思路     我们想要将磁盘...

fileupload上传中文文件名时乱码问题

昨天让这个乱码问题弄了很久,一大早就开始想要怎么解决才好。 很简单上传页面,jsp上传页面代码 1    form action="/struts2/UploadServlet" method...

struts2 使用 jakarta 上传文件时commons fileupload的异常捕捉(转自alfred.w)

struts2 使用 jakarta 上传文件时commons fileupload的异常捕捉(转自alfred.w) 问题:         struts2 使用jakarta ...

jquery.fileupload.js插件使用--多视频或音频上传预览

一、前台代码: jquery.fileupload.js使用测试 .bar { mar...
  • smm188
  • smm188
  • 2016年02月26日 16:02
  • 1842

基于jquery的fileupload上传控件 带进度条、支持多上传、拖拽上传 下载即可用 十分强大 有图

基于jquery的fileupload上传控件 带进度条、支持多上传、拖拽上传 下载即可用 十分强大 有图 源代码下载地址:http://www.zuidaima.com/share/16169345...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:fileupload
举报原因:
原因补充:

(最多只允许输入30个字)