通过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流,将流缓存到内存或磁盘中。当文件过大时,文件将被缓存到内存,否则将被缓存到磁盘的临时文件。最后由服务器端的内存或是临时文件中把文件输出到指定的目录,这个目录才是指定的文件上传目录。

最新文章

  1. drawRect与setNeedsDisplay简单介绍
  2. HTML5+CSS3学习笔记(一)
  3. 网站(logo,主机)
  4. make xconfig时,出现“Unable to find any QT installation&quot;错误
  5. Js练习题之查找字符串中出现最多的字符和个数
  6. html5 drag api详解
  7. windowsUI的总结
  8. 精通CSS高级Web标准解决方案(1-2 层叠与特殊性)
  9. 李洪强iOS开发Swift篇—03_字符串和数据类型
  10. iOS 数据库第三方FMDB的简单使用
  11. Ubuntu离线安装Sogou拼音(附老版本安装&amp;输入法自启动)
  12. spy++捕获窗口消息
  13. html&amp;css学习笔记----YJZJZQA
  14. .NET CORE学习笔记系列(2)——依赖注入[8]: .NET Core DI框架[服务消费]
  15. C#: int 与 byte[] 互转
  16. go goroutine
  17. Docker Kubernetes 介绍 or 工作原理
  18. Codeforces Round #439 C. The Intriguing Obsession
  19. tcp常见状态
  20. JXU1NDRBJXU0RTJBJXU1MjJCJXU1NDI3

热门文章

  1. java后台导出pdf
  2. Linux 中将用户添加到指定组的指令
  3. scoped,会使设置UI组件库的样式识别不出来
  4. BDC备忘
  5. idea中自定义设置xml的头文件的内容
  6. Ajax上传文件/照片时报错TypeError :Illegal invocation
  7. JSON后台处理特殊字符方法,在JSONArray.fromObject转换时处理
  8. python__基础 : 类属性,类方法,静态方法
  9. [Hdu4825]Xor Sum(01字典树)
  10. 奇异值分解(SVD)原理详解及推导