<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form action="UploadServlet3" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td><label for="file1">文件1:</label></td>
<td><input type="file" id="file1" name="file"></td>
</tr>
<tr>
<td><label for="file2">文件2:</label></td>
<td><input type="file" id="file2" name="file"></td>
</tr>
<tr>
<td><label for="file3">文件3:</label></td>
<td><input type="file" id="file3" name="file"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="上传" name="upload"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

 

@MultipartConfig(location = "e:/workspace")
@WebServlet(name = "UploadServlet", urlPatterns = {"/UploadServlet"})
public class UploadServlet extends HttpServlet { /**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//迭代Collection中所有Part对象
for (Part part : request.getParts()) {
//只处理上传文件区段
if (part.getName().startsWith("file")) {
String fileName = getFileName(part);
part.write(fileName);
}
}
} private String getFileName(Part part) {
String header = part.getHeader("Content-Disposition");
String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
header.lastIndexOf("\"");
return fileName;
} // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} /**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} /**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

  

 

使用getParts()上传多个文件的更多相关文章

  1. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...

  2. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  3. SecureCRT上传和下载文件

    SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...

  4. thinkphp如何一次性的上传多个文件,在文件域中可以多选?

    可以做到类似于某度网盘的样式吗? 文件夹的命名, 可以用单数, 也可以用复数, 在同一个项目中, 只要统一就好了. 毕竟项目开发不同于英语写作. 建议使用缩写, 不管是不是缩写都用单数, 这样简洁,容 ...

  5. Drupal8重命名上传的中文名文件

    完整的模块代码文件在Coding.net上,想直接使用的请前往下载:https://coding.net/u/yamus/p/chinese_rename/git/tree/master 最近吧Dru ...

  6. WordPress上传含有中文文件出现乱码

    最近打算学习安装配置WordPress,当然同时也在学习PHP+MySQL,希望以后能做一些关于WordPress定制和二次开发,包括主题和插件.在成功安装WordPress3.5中文版之后,就测试了 ...

  7. 11、只允许在主目录下上传和下载文件,不允许用putty登录

    创建用户xiao,   使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...

  8. 每天一个linux命令(26):用SecureCRT来上传和下载文件

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  9. BatsingJSLib 2.3、Ajax上传多个文件

    //2.3Ajax上传单个或多个文件 //<input type="file" multiple="multiple"/> //参数:文件的表单JD ...

随机推荐

  1. linux设置定时备份mysql数据库

    最近写自己的项目,买了阿里云服务器,可以在云上根据自己想到的需求随意使用技术,感觉很爽.备份mysql流程如下: 环境:CentOS Linux release 7.2.1511 (Core) mys ...

  2. 自己对js对原型链的理解

    js对象分为2种 函数对象和普通对象 函数对象 比如 function Show(){}var x=function Show2(){}var b=new Function("show3&q ...

  3. cavans 详解

    http://blog.csdn.net/clh604/article/details/8536059

  4. JSPatch 使用

    1.JSPatch 准备 地址:https://github.com/bang590/JSPatch 框架:libz.1.tbd , JavaScriptCore.framework 2.cocosp ...

  5. 关于 feature team 的一些内容

    矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...

  6. maven打包jar包

    参考http://www.cnblogs.com/justinzhang/p/4983633.html 新建jar类型的maven project (选择simple project ) 配置 < ...

  7. windows下mysql数据库定时备份。

    注意:看本教程先必须会windows自带的"任务计划程序". 首先创建一个bat后缀的文件我的是timerExecutePhp.bat文件 timerExecutePhp.bat ...

  8. supermap iobect .net 7.1.2 图例的拆分

    LayoutSelection objLytSelect = m_MapLayoutControl.MapLayout.Selection;//.Selection; //LayoutSelectio ...

  9. http://www.cnblogs.com/kissdodog/p/4159176.html

    想要自己一个人完成app,那么后台接口也必须自己动动手.不用担心,其实很简单的,给自己信心!下面就以登录注册为例,做一个api接口 首先在mac上搭建PHP环境,下载 MAMP Pro for Mac ...

  10. .NET开发人员值得关注的七个开源项目 .

    NET开发人员值得关注的七个开源项目 软近几年在.NET社区开源项目方面投入了相当多的时间和资源,不禁让原本对峙的开源社区阵营大吃一惊,从微软.NET社区中的反应来看,微软.NET开发阵营对开源工具的 ...