1,html页面,上传使用input type=file控件,其所在的form必须加上enctype="multipart/form-data"

    <form role="form" id="form-updUser" name="form-updUser"
method="post" action="updUser" enctype="multipart/form-data">
<div class="form-group">
<label for="username">用户名</label> <input type="text"
class="form-control" id="username" name="username"
placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="name">名称</label> <input type="text"
class="form-control" id="name" name="name" placeholder="请输入名称">
</div>
<div class="form-group">
<label for="palce">归属地</label> <select id="select-province"
name="select-province"></select> <select id="select-city"
name="select-city"></select> </label>
</div>
<div class="form-group">
<label for="apartment">部门</label> <input type="text"
class="form-control" id="apartment" name="apartment"
placeholder="请输入部门">
</div>
<div class="form-group">
<label for="phoneNum">联系方式</label> <input type="text"
class="form-control" id="phoneNum" name="phoneNum"
placeholder="请输入联系方式">
</div>
<input type="hidden" name="id">
<div class="form-group">
<label for="inputfile">文件输入</label> <input type="file"
name="file" id="inputfile"> </div>

<input type="hidden" name="id" id="id">
</form>

2,后台借助于两个开源包

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

引入包之后,后台处理的代码如下

private void process(HttpServletRequest request,
HttpServletResponse response) {
// 进行用户更新,以及联系文件的存储
response.setCharacterEncoding("utf-8");
response.setContentType("html;charset=utf-8");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding(request.getCharacterEncoding());
try {
List<FileItem> list = upload.parseRequest(request);
for (int i = ; i < list.size(); i++) {
FileItem item = list.get(i);
if (item.isFormField()) {
// 说明是普通的表单字段
// updUser(item);
// request.getRequestDispatcher("/listUser").forward(request, response);
} else if(!item.getName().equals("")){
// 说明是文件
saveFile(item);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private void saveFile(FileItem item) {
String fileName=item.getName();
//防止文件重名
String imageFileName = new Date().getTime() + new Random().nextInt() +fileName.substring(fileName
.lastIndexOf(".") );
//网站的物理跟路径
String rootPath=this.getServletContext().getRealPath("/");
System.out.println("UpdUser:rootPath:"+rootPath);
String path=rootPath+"files\\";
File dir=new File(path);
if(!dir.exists()){
dir.mkdirs();
}
//将文件写入物理路径
try {
item.write(new File(path,imageFileName));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }

web文件上传的实现的更多相关文章

  1. servlet web文件上传

    web文件上传也是一种POST方式,特别之处在于,需设置FORM的enctype属性为multipart/form-data. 并且需要使用文件域. servlet的代码比较关键是这几句: // 使用 ...

  2. Java Web文件上传

    参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...

  3. WEB文件上传下载功能

    WEB文件上传下载在日常工作中经常用到的功能 这里用到JS库 http://files.cnblogs.com/meilibao/ajaxupload.3.5.js 上传代码段(HTML) <% ...

  4. Web 文件上传 目录

    0. SpringMVC -- 梗概--源码--贰--上传 1. Web上传文件的原理及实现 2. Web文件上传方法总结大全 3. SpringMVC 文件上传配置,多文件上传,使用的Multipa ...

  5. Java Web文件上传原理分析(不借助开源fileupload上传jar包)

    Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web   最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...

  6. WEB文件上传漏洞介绍解决办法

    引用:http://blog.csdn.net/kobejayandy/article/details/35861499 问题: -1. 上传文件WEB脚本语言,服务器的WEB容器解释并执行了用户上传 ...

  7. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

  8. C#实现Web文件上传的两种方法

    1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...

  9. Java web文件上传下载

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52048666 作者:朱培 ID:sdksdk0 邮 ...

  10. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

随机推荐

  1. pycharm 皮肤主题及个性化设置

    1.设置IDE皮肤主题 File -> Settings -> IDE Settings -> Appearance -> Theme -> 选择“Alloy.IDEA ...

  2. Xamarin Visual Studio无法debug

    在Visual Studio中,Target IOS Device下拉框是禁用状态,无法选择. Xamarin论坛中有不少关于这个问题的,如下面这个帖子: http://forums.xamarin. ...

  3. Bigtable: A Distributed Storage System for Structured Data

    https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...

  4. uuu

    #include<stdio.h> int map[10][10]={0}; int step[30]={0}; int max=99999; int q; int ans[99]={0} ...

  5. 迷宫dfs

    #include<stdio.h>int mov1[4]={0,0,1,-1};int mov2[4]={1,-1,0,0};int map[5][5]={0,1,0,0,1,      ...

  6. Lazarus开发平台编译后的可执行程序的减肥方法

    1.在“工程”菜单下,单击“工程选项”,原始状态的界面为: 2.将界面中的两个选项修改为如下: 3.按“确定”按钮,即可 编译以后的可执行程序就将又原来的14m变为1.66m. 1.选择: Proje ...

  7. AP_总体业务及方案

    AP关键业务点说明 关键业务点 说明 预付款余额收回 1. 在应付款管理系统中输入一张虚拟发票,该发票的目的是在系统中冲减对供应商的预付款额,其金额等于预付款的未核销金额,供应商为原供应商. 借:其他 ...

  8. Bootstrap 表单和图片 (内联表单,表单合组,水平排列,复选框和单选框,下拉列表,校验状态,添加额外的图标,控制尺寸,图片)

    一.表单 基本格式 注:只有正确设置了输入框的 type 类型,才能被赋予正确的样式. 支持的输入框控件 包括:text.password.datetime.datetime-local.date.m ...

  9. is not configured for rpc

    exec sp_serveroption @server='myserver', @optname='rpc', @optvalue='true' exec sp_serveroption @serv ...

  10. IE中的fireEvent和webkit中的dispatchEvent

    拿浏览器的click事件来说: 在IE浏览器中如果一个element没有注册click事件,那么直接调用的话会出现异常!当然如果你注册了没有什么可说的. 那么如果使用fireEvent来处理,clic ...