通过common-fileUpload文件上传组件,可以实现上传文档、图片等资料。根据程序的不同要求,它可以有多种方式的应用。

我们这里介绍一种简单的例子,来实现文件上传功能。

准备:

引入相关组建的jar包

首先,我们进入上传页面index.html.

<body>
<form action="./servlet/item/FileUploadServlet" method="post"
enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
<form action="./servlet/HelloWord" method="post">
<input type="submit" />
</form>
<form name="uploadform" method="POST" action="./servlet/item/FileUploadServlet"
ENCTYPE="multipart/form-data">
<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF">
<tr>
<td width="100%" colspan="2">
文件
<input name="x" size="40" type="file">
</td>
</tr>
</table>
<table>
<tr>
<td align="center">
<input name="upload" type="submit" value="开始上传" />
</td>
</tr> </table> </form> </body>

选择要上传的文件,点击开始上传,跳转到servlet页面,即下面提到的FileUploadServlet。

/**
* 实现文件上传的Servlet
* @author Administrator
*
*/
public class FileUploadServlet extends HttpServlet { //上传路径
private File uploadPath; //当文件过大时,需要设置一个临时路径
private File tempPath; public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory();
// 内存存储的最大值
factory.setSizeThreshold(4096); factory.setRepository(tempPath); ServletFileUpload upload = new ServletFileUpload(factory);
//设置文件上传大小
upload.setSizeMax(1000000 * 20);
try {
List fileItems = upload.parseRequest(req);
String itemNo = "";
for (Iterator iter = fileItems.iterator(); iter.hasNext();) {
FileItem item = (FileItem) iter.next(); //是普通的表单输入域
if(item.isFormField()) {
if ("itemNo".equals(item.getFieldName())) {
itemNo = item.getString();
}
}
//是否为input="type"输入域
if (!item.isFormField()) {
String fileName = item.getName();
long size = item.getSize();
if ((fileName == null || fileName.equals("")) && size == 0) {
continue;
}
//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
//item.write(new File(uploadPath + itemNo + ".gif"));
item.write(new File(uploadPath, itemNo + ".gif"));
}
}
//重定向页面
res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 初始化方法,设定目录
*/
public void init() throws ServletException {
uploadPath = new File(getServletContext().getRealPath("upload"));
//System.out.println("uploadPath=====" + uploadPath);
//如果目录不存在
if (!uploadPath.exists()) {
//创建目录
uploadPath.mkdir();
} //临时目录
tempPath = new File(getServletContext().getRealPath("temp"));
if (!tempPath.exists()) {
tempPath.mkdir();
}
}

小结:

FileUpload上传一个文件的过程中可以分为三个部分:首先,客户端与服务器端建立连接,把要上传的文件生成request数据流。其次服务器端接收request流,将流缓存到内存或磁盘中。当文件过大时,文件将被缓存到内存,否则将被缓存到磁盘的临时文件。最后由服务器端的内存或是临时文件中把文件输出到指定的目录,这个目录才是指定的文件上传目录。

使用common-fileUpload实现文件上传的更多相关文章

  1. zt对于C#中的FileUpload解决文件上传大小限制的问题设置

    对于C#中的FileUpload解决文件上传大小限制的问题设置 您可能没意识到,但对于可以使用该技术上载的文件的大小存在限制.默认情况下,使用 FileUpload 控件上载到服务器的文件最大为 4M ...

  2. 使用fileupload实现文件上传

    一. fileupload组件工作原理 先来张图片, 帮助大家理解 fileupload核心API 1. DiskFileItemFactory构造器1) DiskFileItemFactory() ...

  3. asp.net web常用控件FileUpload(文件上传控件)

    FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮. 常用的属性:FileBytes,FileContent.FileName.HasFile.PostedFi ...

  4. zk FileUpload(文件上传)

    <button label="上传 Image" upload="true,maxsize=1073741824"> <attribute n ...

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

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

  6. extjs采用fileupload进行文件上传后台实现

    前台js: form: Ext.define("GS.base.BasicImportForm",{    extend:"Ext.form.Panel",   ...

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

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

  8. Apache Commons FileUpload 实现文件上传

    Commons FileUpload简介 Apache Commons是一个专注于可重用Java组件开发的 Apache 项目.Apache Commons项目由三个部分组成: 1.Commons P ...

  9. 完成FileUpload的文件上传功能,且可改按钮样式

    FileUpload控件: 更改按钮样式思路: 自己定义一个按钮,设置该按钮的样式,然后将FileUpload控件通过定位定在自己定义的按钮上面,设置z-index,使得控件浮在自己定义的按钮上面,记 ...

  10. FileUpload实现文件上传(包含多文件)

    package com.hzml.serve; import java.io.File; import java.io.IOException; import java.io.PrintWriter; ...

随机推荐

  1. 字符串缓冲区StringBuffer类,正则表达式

    StringBuffer类 StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容.它是一个容器,容器中可以装很多字符串. ...

  2. Unexpected token o in JSON at position 1

    ajax返回的数据已经是object格式,无需再使用“var newjsonObj = JSON.parse(jsonObj)” 进行转换.

  3. 微信H5单页面滑动的时候如何避免出界,出现头部和底部的黑底?

    ios系统微信浏览器.safari浏览器中h5页面上拉下滑导致悬浮层脱离窗口的解决方法 ios偶现下拉出现黑底时,界面第一次上拉时拉不动的解决方案: document.querySelector('# ...

  4. 网际协议 IP

    网际协议 网际协议(internet  protocol),简称IP; 概念:TCP/IP网络体系结构中网际层的协议.用以提供无连接的数据服务. 1.IP地址的概念及组成 概念:IP地址就是用来唯一标 ...

  5. 爱她就用python给她画个小心心 ♥(ˆ◡ˆԅ)

    from turtle import * a = Turtle() screensize(400, 300, "blue") setup(width=1300, height=65 ...

  6. 裸机——ADC

    1.首先是ADC的基本知识 模拟信号,连续的 数字信号,离散的 模拟信号,现实世界的很多东西都是连续的,所以使用模拟信号才能准确描述,但是模拟信号不方便控制. 数字信号,计算机中的信号大都为数字的,数 ...

  7. python——集合

    在python中,字典的亲戚就是集合,集合就是无映射关系的字典,花括号并不是字典的特权.如下面程序所示: >>> num = {} >>> type(num) &l ...

  8. awk命令例子详解

    awk -F: '{print "Number of dields: "NF}' passwd 字段分隔符设为冒号,所以每条记录的字段数变成7: awk  '{print &quo ...

  9. python基础之闭包函数和装饰器

    补充:全局变量声明及局部变量引用 python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 global关键字用来在函数或其 ...

  10. 11,flask之--WTForms

    WTForms是什么? 相当于django的ModelForm. 在网页中,为了和用户进行信息交互总是不得不出现一些表单.flask设计了WTForm表单库来使flask可以更加简便地管理操作表单数据 ...