文件上传

使用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. day 93 Restframwork

    苑昊博客: http://www.cnblogs.com/yuanchenqi/articles/7570003.html  一.queryset 特性 from django.db import m ...

  2. redis内存监控与回收

    Redis有自己的内存分配器,当key-value对象被移除时,Redis不会马上向操作系统释放其占用内存.redis之所以这样的设计有两个原因. OS可能会将释放内存交换到虚拟内存,但OS的虚拟内存 ...

  3. webpack快速入门——webpack3.X 快速上手一个Demo

    1.进入根目录,建两个文件夹,分别为src和dist 1).src文件夹:用来存放我们编写的javascript代码,可以简单的理解为用JavaScript编写的模块. 2).dist文件夹:用来存放 ...

  4. 【Hight Performance Javascript】——脚本加载和运行

    脚本加载和运行 当浏览器遇到一个<script>标签时,无法预知javascript是否在<p>标签中添加内容.因此,浏览器停下来,运行javascript代码,然后继续解析. ...

  5. (转)C# Enum,Int,String的互相转换 枚举转换--非常实用

    Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基 ...

  6. 在操作Centos系统时经常出现You have new mail in /var/spool/mail/root提示怎么回事?

    例如,在命令窗口中输入date查看时间,下面会出现一行提示 实际上,该功能为Linux操作系统核对系统资源状态并汇总,默认发送到root用户的/var/spool/mail/root目录,并在标准输出 ...

  7. 剑指offer三十八之二叉树的深度

    一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...

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

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

  9. Oracle 锁问题处理

    Oracle 锁问题处理 锁等待问题是一个常见的问题 查看持有锁的对象 查看事务正在执行的语句,与应用确认是否能够kill kill 对应的session

  10. Java之集合(八)HashMap

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7338372.html 1.前言 本章介绍Java中最常用的一个集合类HashMap,此类在不同的JDK版本有不同 ...