文件上传

使用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使用及问题的更多相关文章

  1. 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 ...

  2. Apache Commons fileUpload实现文件上传之一

      需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...

  3. 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

    最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...

  4. Spring MVC使用commons fileupload实现文件上传功能

    通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...

  5. commons.fileupload简单应用

    导入包: commons-fileupload-1.3.1.jar commons-io-2.4.jar commons-fileupload依赖于commons-io,commons-io-2.4必 ...

  6. JSP 文件上传下载系列之二[Commons fileUpload]

    前言 关于JSP 文件上传的基础和原理在系列一中有介绍到. 这里介绍一个很流行的组件commons fileupload,用来加速文件上传的开发. 官方的介绍是:  让添加强壮,高性能的文件到你的se ...

  7. Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  8. org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

    1.错误原因 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't ...

  9. 上传文件代码报错,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]    : ...

  10. Apache Commons FileUpload 实现文件上传

    Commons FileUpload简介 Apache Commons是一个专注于可重用Java组件开发的 Apache 项目.Apache Commons项目由三个部分组成: 1.Commons P ...

随机推荐

  1. neutron openvswitch + vxlan 通讯

  2. (1)RGB-D点云生成

    bin文件夹下为生成的可执行文件generate_cloud,执行时和data文件放在同一文件夹下. 图像数据来自小觅相机. src下的源码,包括generatePointCloud.cpp和CMak ...

  3. powerDesiner设计数据库的一些用法

    数据库的设计主要有以下几个步骤: 1:需求分析:根据业务需求分析出满足客户的需求,从而建立相应的数据库 2:概念设计:通过数据抽象,设计系统概念模型,一般为E-R模型:(entity-relation ...

  4. Linux 基础命令 持续更新中...

    1.ls 显示当前文件/文件夹 显示文件大小: ls -lh  显示隐藏文件: ls -a 显示文件详细信息: ls -l (ll)2.pwd 显示当前所在路径 cat 显示当前文件下所有内容3.cd ...

  5. Ant运行build.xml执行服务器scp,异常解决jsch.jar

    公司ant打包上线 一直出现这个问题. Ant运行build.xml执行服务器scp,异常解决jsch.jar BUILD FAILEDD:\eclipse\eclipse-jee-luna-SR2- ...

  6. python实现百度地图API获取某地址的经纬度

    申请百度API 1.打开网页 http://lbsyun.baidu.com/index.php?title=首页 选择功能与服务中的地图,点击左边的获取密匙,然后按照要求申请即可,需要手机和百度账号 ...

  7. IP等级

    IP是Ingress Protection的缩写,IP等级是针对电气设备外壳对异物侵入的防护等级,来源是国际电工委员会的标准IEC 60529,这个标准在2004年也被采用为美国国家标准.  在这个标 ...

  8. Xshell 命令后台执行

    但是这样没有在后台启动:因此sh那一行代码需要修改: 前边加上nohup 后边加上& nohup dotnet helloword.dll & 然后,进程启动之后,按任意键进入输入状态 ...

  9. PHP基础记录

    1. require和require_once的区别 require_once()包涵是绝对路径 include() 和require() :语句包括并运行指定文件. include() 产生一个警告 ...

  10. Xpath string()提取多个子节点中的文本

    <div> <ul class="show"> <li>275万购昌平邻铁三居 总价20万买一居</li> <li>00 ...