commons-fileupload-1.4使用及问题
文件上传
使用commons-fileupload-1.4控件及依赖的commons-io-2.6控件
jsp页面中内容
<form action="../servlet/FileUpdate" method="post" enctype="multipart/form-data">
<div align="center">
<fieldset style="width:80%">
<legend>上传文件</legend><br/>
<div align="left">上传文件1</div>
<div align="left">
<input type="file" name="file1"/>
</div> <div>
<div align='left'>
<input type='submit' value="上传文件"/>
</div>
</div>
</fieldset>
</div>
</form>
servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
// Parse the request
String saveName = "";
try {
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
if(item.isFormField()){//如果只是表单中信息,不是表单文件
String fieldName = item.getFieldName();
String fieldValue = item.getString();
out.print("<br>fieldName: "+fieldName+",--fieldValue: "+fieldValue);
}else{
InputStream inputStream = item.getInputStream();
//得到保存文件的路径
String realpath=this.getServletContext().getRealPath("update");
//得到上传的文件的名字,可能显示的是路径,所以需要取出文件名
String allFilePath = item.getName();
//getName()值为绝对路径!!!下面代码转换取文件名
String fileName = null;
int ind = allFilePath.lastIndexOf("\\");
if (ind != -1) {
fileName = allFilePath.substring(ind + 1);
}else {
fileName = allFilePath;
} out.print("<br>上传的文件名: "+fileName);
//读取的不能是目录,应该加上文件名
File file=new File(realpath + "\\" + fileName);
if(file.getParentFile().exists()){
file.createNewFile();//创建文件
}else {
file.getParentFile().mkdirs();//创建父级文件路径
file.createNewFile();//创建文件
}
FileOutputStream fos=new FileOutputStream(file);
byte[] bytes= new byte[1024];
int len=0;
//写入文件
while((len=inputStream.read(bytes))!=-1){
fos.write(bytes, 0, len);
} inputStream.close();
fos.close();
out.print("<h3>"+allFilePath+"文件上传成功</h3>");
}
} } catch (FileUploadException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(saveName);
}
}
web.xml
<servlet>
<servlet-name>FileUpdate</servlet-name>
<servlet-class>com.oneself.shopping.servlet.FileUpdate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUpdate</servlet-name>
<url-pattern>/servlet/FileUpdate</url-pattern>
</servlet-mapping>
问题
java.io.FileNotFoundException: D:\update (拒绝访问。)
FileOutputStream读取流的时候如果是文件夹,就会出此错误。读取的目录后面要加文件名,如下:
File file=new File(realpath + "\\" + fileName);
if(file.getParentFile().exists()){
//file.getParentFile().mkdirs();//创建父级文件路径
file.createNewFile();//创建文件
System.out.println(file.exists());
}else {
file.getParentFile().mkdirs();//创建父级文件路径
file.createNewFile();//创建文件
}
FileOutputStream fos=new FileOutputStream(file);
commons-fileupload-1.4使用及问题的更多相关文章
- CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries、Apache Commons FileUpload and Apache Tomcat DoS
catalog . Description . Analysis . POC . Solution 1. Description MultipartStream.java in Apache Comm ...
- Apache Commons fileUpload实现文件上传之一
需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...
- Spring MVC使用commons fileupload实现文件上传功能
通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...
- commons.fileupload简单应用
导入包: commons-fileupload-1.3.1.jar commons-io-2.4.jar commons-fileupload依赖于commons-io,commons-io-2.4必 ...
- JSP 文件上传下载系列之二[Commons fileUpload]
前言 关于JSP 文件上传的基础和原理在系列一中有介绍到. 这里介绍一个很流行的组件commons fileupload,用来加速文件上传的开发. 官方的介绍是: 让添加强壮,高性能的文件到你的se ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException
1.错误原因 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't ...
- 上传文件代码报错,java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
2018-09-11 11:11:08.235 ERROR 14352 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : ...
- Apache Commons FileUpload 实现文件上传
Commons FileUpload简介 Apache Commons是一个专注于可重用Java组件开发的 Apache 项目.Apache Commons项目由三个部分组成: 1.Commons P ...
随机推荐
- js:实现自定义事件对象接口
网易2017内推笔试题 要求: 请实现下面的自定义事件Event对象的接口,功能见注释(测试1) 该Event对象的接口需要能被其他对象拓展复用(测试2) //测试1 Event.on('test', ...
- JavaScript编程题(含腾讯2016校招题)
作者:ManfredHu 链接:http://www.manfredhu.com/2016/04/02/15-veryGoodForUsing/ 声明:版权所有,转载请保留本段信息,否则请不要转载 几 ...
- Android Studio - Unable to create Debug Bridge: Unable to start adb server: adb server version (32) doesn't match this client (40)
错误提示:Unable to create Debug Bridge: Unable to start adb server: adb server version (32) doesn't matc ...
- TCP BBR - 一键开启脚本
这是秋水逸冰提供的TCP BBR一键开启脚本,脚本详细说明地址: https://teddysun.com/489.html 按照说明操作就可以了,注意问题:这个脚本获取有的时候可能有问题,如果提示脚 ...
- jzoj5928
tj:題解裡公式是錯的 我們可以考慮每一個節點[a,a+2^b-1]對答案的貢獻 則當這個節點是左兒子時,貢獻為2^b 是右兒子時,貢獻為2n−a−2b+12^n-a-2^b+12n−a−2b+1 左 ...
- 前端入门CSS(3)
day60 不透明度 opacity()\ opacity (不透明度) 1. 取值0~1 2. 和rgba()的区别: ...
- 用Python玩转数据——第五周数据统计和可视化
一.数据获取 1.本地数据 with 语句,pd.read_csv('data.csv') 2.网站上数据 2.1 直接获取网页源码,在用正则表达式进行删选 2.2 API接口获取---以豆瓣为例 i ...
- 社区发现的3个评估指标:标准化互信息NMI,ARI指标,以及模块度(modularity)
转载请注明出处:http://www.cnblogs.com/bethansy/p/6890972.html 一.已知真实社区划分结果 1.NMI指数,互信息和标准化互信息 具体公式和matlab代码 ...
- ThreadLocal的实现机制
TLS(Thread Local Storage)通过分配更多内存来解决多线程对临界资源访问的互斥问题,即每个线程均自己的临界资源对象, 这样也就不会发生访问冲突,也不需要锁机制控制,比较典型的以空间 ...
- 剑指offer六十一之序列化二叉树(待补充)
一.题目 请实现两个函数,分别用来序列化和反序列化二叉树二.思路 三.代码 --------------------------------------------- 参考链接: