文件上传

使用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 21 序列化模块_Json,Pickle,Shelve

    序列化 , 数据类型,列表 元组, 字符串 只有字符串能被写入文件中. 能在网络上传输的只能是bytes - 字符串 把要传输的和要存储的内容转换成字符串. 字符串 转换回 要传输和存储的内容 序列化 ...

  2. FFmpeg编写的代码

    //初始化解封装    av_register_all();    avformat_network_init();    avcodec_register_all();    //封装文件的上下文  ...

  3. 为什么子元素设置margin-top会作用在父元素上?

    原因在于:CSS 外边距合并 复现: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. gitlab改变服务器ip

    一./home/git/gitlab/config/unicorn.rb 其中的ip需要修改,否则unicorn无法启动. 二./home/git/gitlab-shell/config.yml 其中 ...

  5. 向 webview 添加 userScript

    添加前,网页代码如下: <!DOCTYPE html> <html> <!--<script type="text/javascript"> ...

  6. ffmpeg开发基础知识

    1.音频采集 面临的问题:延时敏感,噪声消除,回声消除,静音检测 主要参数: 采样率,位宽,声道数,音视频帧 采样率: 也称为采样速度或者采样率,定义了每秒从连续信号中提取并组成离散信号的采样个数,它 ...

  7. Spring 事务传播行为实验

    一.Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为.有以下选项可供使用: PROPAGATION_REQUIRED--支持当前事务,如果当前 ...

  8. Window Location对象

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. window.location 对象在编写时可不使用 window 这个前缀. Location ...

  9. Oracle 数据库维护管理之--dbms_lock

    1.查询相关的v$视图,但是提示表或视图不存在解决办法     原因是使用的用户没有相关的查询权限导致 解决办法: grant select  any dictionary to 用户;    --这 ...

  10. [经验]微信开放平台,一个APP secret可以绑定一个APP,然后再绑定一个ipad 版本APP

    微信开放平台,一个APP secret可以绑定一个APP,然后再绑定一个ipad 版本APP