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 ...
随机推荐
- 关于win10 链接安卓设备报错winusb.sys未经签名的解决办法
很简单,各位,我找了一个签过名的winusb.sys替换原来的文件即可. 操作系统win10 64位专业版(更新到最新版本了) 网盘地址 安装好以后,就没有那个惊叹号咯!
- 《Python绝技:运用Python成为顶级黑客》 用Python进行取证调查
1.你曾经去过哪里?——在注册表中分析无线访问热点: 以管理员权限开启cmd,输入如下命令来列出每个网络显示出profile Guid对网络的描述.网络名和网关的MAC地址: reg query &q ...
- C/C++ 语言 Hello world
#include <stdio.h> void main() { int x,i; ; scanf("%d",&x); if(x>y) printf(&q ...
- hdoj1068 Girls and Boys(二分图的最大独立集)
题意:有n个人,要彼此认识.选择一个集合,使得集合里的每个人相互不认识.求集合中人数的最大值. 求二分图的最大独立集. 公式:最大独立集=顶点数-最大匹配 这个题目中因为集合是一个,所以求出最大匹配数 ...
- 从 falcon api 中获取数据
import json import time import requests openfalcon = 'http://ip:port/api/v1' user = 'user' password ...
- spring3.2+mybatis3.2+maven整合
用maven管理spring+mybatis的项目: 这里主要讲述的是maven中的pom.xml文件的配置,以及在maven构建过程中会碰到的几个问题(我用的是maven4.4的版本): 首先一步一 ...
- iOS--MJRefresh的使用 上拉刷新和下拉加载
1.一般使用MJRefresh 来实现上拉刷新和下拉加载功能 2.MJRefresh 下载地址:https://github.com/CoderMJLee/MJRefresh 3. MJRefresh ...
- vue项目打包后路径出错
安装完vue后搭建了一个项目,直接执行 npm run dev 是可以正常打开页面的: 但是执行 npm run build 打包项目后打开却报错了,如下: 原来是项目中的静态文件路径报错了... 然 ...
- mysql优化器在统计全表扫描的代价时的方法
innodb 的聚集索引 的叶子结点 存放的 是 索引值以及数据页的偏移量 那么在计算全表扫描的代价是怎么计算的呢? 我们知道代价 为 cpu代价+io代价 cpu代价 就是 每5条记录比对 计算一个 ...
- hadoop1.x异常
可能是存在class冲突 http://stackoverflow.com/questions/25130799/hadoop-java-io-ioexception-call-to-localhos ...