web文件上传的实现
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文件上传的实现的更多相关文章
- servlet web文件上传
web文件上传也是一种POST方式,特别之处在于,需设置FORM的enctype属性为multipart/form-data. 并且需要使用文件域. servlet的代码比较关键是这几句: // 使用 ...
- Java Web文件上传
参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...
- WEB文件上传下载功能
WEB文件上传下载在日常工作中经常用到的功能 这里用到JS库 http://files.cnblogs.com/meilibao/ajaxupload.3.5.js 上传代码段(HTML) <% ...
- Web 文件上传 目录
0. SpringMVC -- 梗概--源码--贰--上传 1. Web上传文件的原理及实现 2. Web文件上传方法总结大全 3. SpringMVC 文件上传配置,多文件上传,使用的Multipa ...
- Java Web文件上传原理分析(不借助开源fileupload上传jar包)
Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web 最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...
- WEB文件上传漏洞介绍解决办法
引用:http://blog.csdn.net/kobejayandy/article/details/35861499 问题: -1. 上传文件WEB脚本语言,服务器的WEB容器解释并执行了用户上传 ...
- java web 文件上传下载
文件上传下载案例: 首先是此案例工程的目录结构:
- C#实现Web文件上传的两种方法
1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...
- Java web文件上传下载
[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52048666 作者:朱培 ID:sdksdk0 邮 ...
- springboot+web文件上传和下载
一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...
随机推荐
- LightOJ 1188 Fast Queries(简单莫队)
1188 - Fast Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...
- 20. 求阶乘序列前N项和
求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...
- Web 在线文件管理器学习笔记与总结(7)重命名文件
rename($oldname,$newname) 重命名文件或目录 <<<EOF EOF; 使用heredoc 技术,来部分实现界面与代码的准分离 重命名时,需要验证新文件名的合法 ...
- Xamarin 示例Standard Controls报错:xamarin Failed to compile interface file. See Build Output for details
Standard Controls 示例下载地址: http://developer.xamarin.com/content/StandardControls/ Xamarin官网上的IOS示例“St ...
- Jquery--array
--遍历数组 $.each(Array, function(i, value) { this; //this指向当前元素 i; //i表示Array当前下标 value; //value表示Array ...
- 7.PHP内核探索:Apache模块介绍
Apache概述 Apache是目前世界上使用最为广泛的一种Web Server,它以跨平台.高效和稳定而闻名.按照去年官方统计的数据,Apache服务器的装机量占该市场60%以上的份额.尤其是在 X ...
- What's Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor - first pass.dll Error Guide
If you've found yourself here, I'm guessing that you're getting Assembly - CSharp - Editor - first p ...
- [qemu] 在前端驱动使用virtio的情况下,如何让后端使用vhost-user [未解决]
首先,如果你更关心原理和知识,请读读这个 http://chuansong.me/n/2186528 (值得细细的逐字读). 在<<深入浅出dpdk>>中提到,vhost-us ...
- 窗口类型(Widget, Window, Dialog, Desktop, SubWindow等等)
http://doc.qt.io/qt-5/qwidget.html#windowFlags-prop http://doc.qt.io/qt-5/qtwidgets-widgets-windowfl ...
- AutoHotKey使用:空格键坏了怎么办?
;RCtrl:: RAlt:: Send, {space} Return http://ahkscript.org/ https://autohotkey.com/download/