FileUpload实现文件上传(包含多文件)
package com.hzml.serve; import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; @WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
private String filePath; // 文件存放目录
private String tempPath; // 临时文件目录
// 初始化
public void init() throws ServletException
{
super.init();
// 从配置文件中获得初始化参数
ServletContext context = getServletContext();
filePath = context.getRealPath("/") + "/hzmlFile";
tempPath = context.getRealPath("/") + "/hzmlFile";
System.out.println("文件存放目录、临时文件目录准备完毕 ...");
} private void process(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
res.setContentType("text/plain;charset=gbk");
PrintWriter pw = res.getWriter();
try{
DiskFileItemFactory diskFactory = new DiskFileItemFactory();
// threshold 极限、临界值,即硬盘缓存 1M
diskFactory.setSizeThreshold(4 * 1024);
// repository 贮藏室,即临时文件目录
diskFactory.setRepository(new File(tempPath)); ServletFileUpload upload = new ServletFileUpload(diskFactory);
// 设置允许上传的最大文件大小 4M
upload.setSizeMax(4 * 1024 * 1024);
// 解析HTTP请求消息头
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
while(iter.hasNext())
{
FileItem item = (FileItem)iter.next();
if(item.isFormField())
{
System.out.println("处理表单内容 ...");
processFormField(item, pw);
}else{
System.out.println("处理上传的文件 ...");
processUploadFile(item, pw);
}
}// end while() pw.close();
}catch(Exception e){
System.out.println("使用 fileupload 包时发生异常 ...");
e.printStackTrace();
}// end try ... catch ...
} public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
process(req, res);
} protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
process(req, res);
} // 处理表单内容
private void processFormField(FileItem item, PrintWriter pw)
throws Exception
{
String name = item.getFieldName();
String value = item.getString();
pw.println(name + " : " + value + "\r\n");
} // 处理上传的文件
private void processUploadFile(FileItem item, PrintWriter pw)
throws Exception
{
// 此时的文件名包含了完整的路径,得注意加工一下
String filename = item.getName();
System.out.println("完整的文件名:" + filename);
int index = filename.lastIndexOf("\\");
filename = filename.substring(index + 1, filename.length()); long fileSize = item.getSize(); if("".equals(filename) && fileSize == 0)
{
System.out.println("文件名为空 ...");
return;
}
pw.println(filePath);
File uploadFile = new File(filePath + "/" + filename);
item.write(uploadFile);
pw.println(filename + " 文件保存完毕 ...");
pw.println("文件大小为 :" + fileSize + "\r\n");
}
}
FileUpload实现文件上传(包含多文件)的更多相关文章
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- struts文件上传(多文件)
第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- yii2.0单文件上传和多文件上传
yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传: yii2单个文件上传: 上传步奏,先创建上传表单模型model(包含验证规 ...
- 使用PHP实现文件上传和多文件上传
PHP 2013 年 9 月 4 日 暂无评论 在PHP程序开发中,文件上传是一个使用非常普遍的功能,也是PHP程序员的必备技能之一.值得高兴的是,在PHP中实现文件上传功能要比在Java.C#等语言 ...
- Struts1文件上传、单文件、多文件上传【Struts1】
将struts1文件上传的操作汇总了一下,包括单文件上传和多文件上传,内容如下,留作备忘: Struts2实现文件上传的文章(http://blog.csdn.net/itwit/article/d ...
- 强大的支持多文件上传的jQuery文件上传插件Uploadify
支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...
- skymvc文件上传支持多文件上传
skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...
- apache fileupload 文件上传,及文件进度设置获取
文件上传action处理: boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) ...
随机推荐
- 我的github代码库
我的github代码库地址:https://github.com/gooree.Enjoy coding,enjoy sharing.
- Ajax的同步与异步
原文地址:http://www.cnblogs.com/Joetao/articles/3525007.html <%@ Page Language="C#" AutoEve ...
- Android热修复技术选型(不在市场发布新版本的情况下,直接更新app)
2015年以来,Android开发领域里对热修复技术的讨论和分享越来越多,同时也出现了一些不同的解决方案,如QQ空间补丁方案.阿里AndFix以及微信Tinker,它们在原理各有不同,适用场景各异,到 ...
- 再探@font-face及webIcon制作
@font-face 不能说他是什么新东西了,在 CSS2.0 规范中就有了这玩意儿,IE4.0 开始就已经出现,只是当时用的不是特别广泛,后来在 CSS2.1 草案中又被删掉.随着 web 的急速发 ...
- git版本管理策略及相关技巧(A)
公司几乎所有的项目都是使用 git 仓库来管理代码,以前对 git 只有些肤浅的了解,每次提交代码或者上线的时候总是会提心吊胆,生怕出现一些未知的问题.经过三个月的踩坑和填坑, git 操作颇显成熟. ...
- Visual Studio 2012 Visual C++ 入门
改进的C++11标准的支持 标准模板库 为STL添加了新的库文件:<atomic>.<chrono>.<condition_variable>.<filesy ...
- SQL Server内存理解的误区
SQL Server内存理解 内存的读写速度要远远大于磁盘,对于数据库而言,会充分利用内存的这种优势,将数据尽可能多地从磁盘缓存到内存中,从而使数据库可以直接从内存中读写数据,减少对机械磁盘的IO请求 ...
- shell脚本常规技巧
邮件相关 发送邮件: #!/usr/bin/python import sys; import smtplib; from email.MIMEText import MIMEText mail_ho ...
- js模版引擎handlebars.js实用教程——each-基本循环使用方法
返回目录 <!DOCTYPE html> <html> <head> <META http-equiv=Content-Type content=" ...
- STSdb,最强纯C#开源NoSQL和虚拟文件系统 4.0 RC2 支持C/S架构
STSdb是什么 再来说明一下STSdb是什么:STSdb是C#写的开源嵌入式数据库和虚拟文件系统,支持实时索引,性能是同类产品的几倍到几十倍,访问官方网站. 温故知新 之前发了文章<STSdb ...