导入包:

commons-fileupload-1.3.1.jar

commons-io-2.4.jar

commons-fileupload依赖于commons-io,commons-io-2.4必须是JDK6+;

下载地址:

http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi

http://commons.apache.org/proper/commons-io/download_io.cgi


index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1> <form name="myform" action="FileUploadTestServlet" method="post" enctype="multipart/form-data">
姓名: <br>
<input type="text" name="name" size="15"><br>
附件1:<br>
<input type="file" name="myfile1"><br>
附件2:<br>
<input type="file" name="myfile2"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
FileUploadTestServlet.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; /**
*
* @author y
*/
public class FileUploadTestServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); if(!ServletFileUpload.isMultipartContent(request)){
return;
} FileItemFactory fileFactory = new DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload(fileFactory); try {
List items = fileUpload.parseRequest(request);
Iterator iter = items.iterator(); while(iter.hasNext()){
FileItem item = (FileItem)iter.next();
if(item.isFormField()){
out.print("<p>"+item.getFieldName()+":"+new String(item.getString().getBytes("ISO8859-1"), "utf-8")+"</p>");
}else{
String path = "/home/y/my_temp/upload"+File.separator+item.getName();
File disk = new File(path);
item.write(disk); out.print("<p>文件保存到:"+path+"</p>");
}
}
} catch (FileUploadException ex) {
Logger.getLogger(FileUploadTestServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(FileUploadTestServlet.class.getName()).log(Level.SEVERE, null, ex);
}finally{
out.close();
} } @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} }

commons.fileupload简单应用的更多相关文章

  1. Apache Commons fileUpload实现文件上传之一

      需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...

  2. JSP 文件上传下载系列之二[Commons fileUpload]

    前言 关于JSP 文件上传的基础和原理在系列一中有介绍到. 这里介绍一个很流行的组件commons fileupload,用来加速文件上传的开发. 官方的介绍是:  让添加强壮,高性能的文件到你的se ...

  3. Commons FileUpload

    转载自(https://my.oschina.net/u/2000201/blog/486744) 1    概述 Commons FileUpdate包很容易为你的Servlet和web应用程序添加 ...

  4. 使用Commons FileUpload 1.3.3和Servlet 3.0上传文件

    简介 Commons FileUpload可以轻松地为web应用程序添加强大,高性能的文件上传功能.Servlet3.0之前的web应用程序需要使用Commons FileUpload组件上传文件,但 ...

  5. CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries、Apache Commons FileUpload and Apache Tomcat DoS

    catalog . Description . Analysis . POC . Solution 1. Description MultipartStream.java in Apache Comm ...

  6. 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

    最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...

  7. Spring MVC使用commons fileupload实现文件上传功能

    通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...

  8. Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  9. org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

    1.错误原因 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't ...

随机推荐

  1. Loading Cargo

    Loading Cargo "Look Stephen, here's a list of the items that need to be loaded onto the ship. W ...

  2. hdu1028:整数拆分

    求整数的拆分数.. 一种解法是母函数 #include <iostream> #include <stdio.h> #include<string.h> #incl ...

  3. thinkphp连接mysql5.5版本数据库

    //数据库配置信息 'DB_TYPE' => 'mysqli', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => ' ...

  4. Lucene 高亮功能

    原文转载自: http://qindongliang1922.iteye.com/blog/1953409 高亮功能一直都是全文检索的一项非常优秀的模块,在一个标准的搜索引擎中,高亮的返回命中结果,几 ...

  5. .net网站开发(一):1.input表单元素

    其实,在半年前我对网站开发还是完全不感冒的,不是没认识,而是只认识到表面.我以为网站模型就那几样,新闻.论坛.博客啥的,仿个站出来有什么意思?但现在我是知道了,大多应用开发还是采用B/S架构的,包括服 ...

  6. core_cm3文件函数一览

    core_cm3是ARM公司推出来的统一规定,这是对下游芯片厂商的统一规定,因此可以再Cortex-M3(CM3)之间进行移植.此文件中定义了一些对特殊功能寄存器的C语言形式的操作,本质上是内敛汇编和 ...

  7. C#基础:集合

        C#中的数组实现为 System.Array 类的实例,它们只是集合类(Collection Classes)中的一种类型.集合类一般用于处理对象列表,其功能比简单数组要多,功能大多是通过实现 ...

  8. SpringTest2

    Spring 框架第二天 AOP切面编程 今天重点内容: 1. 什么是AOP ? AOP实现原理是怎样的? AOP相关术语 2. AOP底层实现 (了解) ----- JDK动态代理. Cglib动态 ...

  9. 华为oj - 统计大写字母个数

    练手而已. 给初学者参考 #include <stdio.h> #include <string.h> int main(void) { char string[200]={' ...

  10. oc随笔四:NSString、NSNumber

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...