一.组件准备css
Servlet实现文件上传,须要添加第三方提供的jar包
commons-fileupload-1.2.2-bin.zip
commons-io-2.3-bin.zip
在http://commons.apache.org/downloads/index.html找,而后放在以下目录下html
文件上传的表单提交方式必须是POST方式,
编码类型:enctype="multipart/form-data",默认是 application/x-www-form-urlencodedjava
文件上传
具体步骤:
* 1)得到磁盘文件条目工厂 DiskFileItemFactory 要导包
* 2) 利用 request 获取 真实路径 ,供临时文件存储,和 最终文件存储 ,这两个存储位置可不一样,也可相同
* 3)对 DiskFileItemFactory 对象设置一些 属性
* 4)高水平的API文件上传处理 ServletFileUpload upload = new ServletFileUpload(factory); 目的是调用 parseRequest(request)方法 得到 FileItem 集合list (一次上传多个文件,意思是多个input file)
* 5)在 FileItem 对象中 获取信息, 遍历, 判断 表单提交过来的信息 是不是 普通文本信息 另作处理
* 6)
* 第一种. 用第三方 提供的 item.write( new File(path,filename) ); 直接写到磁盘上
* 第二种. 手动处理
apache
二.理论准备数组
base标签只能放置在head标签内,只有href和target,全部浏览器都支持head标签,里面的title是惟一必须的。<base> 标签为页面上的全部连接规定默认地址或默认目标。一般状况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白。使用 <base> 标签能够改变这一点。浏览器随后将再也不使用当前文档的 URL,而使用指定的基本 URL 来解析全部的相对 URL。这其中包括 <a>、<img>、<link>、<form> 标签中的 URL。浏览器
<html><head><base href="http://www.w3school.com.cn/i/" /><base target="_blank" /></head><body><img src="eg_smile.gif" /><br /><p>请注意,咱们已经为图像规定了一个相对地址。因为咱们已经在 head 部分规定了一个基准 URL,浏览器将在以下地址寻找图片:</p><p>"http://www.w3school.com.cn/i/eg_smile.gif"</p><br /><br /><p><a href="http://www.w3school.com.cn">W3School</a></p><p>请注意,连接会在新窗口中打开,即便连接中没有 target="_blank" 属性。这是由于 base 元素的 target 属性已经被设置为 "_blank" 了。</p></body></html>
三.代码实现缓存
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获得项目的名字,若是项目为根目录,则获得一个"",即空的字条串String path = request.getContextPath();//scheme:httpString basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><!--
全部浏览器都支持head标签,里面的title是惟一必须的。<base> 标签为页面上的全部连接规定默认地址或默认目标。一般状况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白。使用 <base> 标签能够改变这一点。浏览器随后将再也不使用当前文档的 URL,而使用指定的基本 URL 来解析全部的相对 URL。这其中包括 <a>、<img>、<link>、<form> 标签中的 URL。--><base href="<%=basePath%>"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件上传</title></head><body><form action="2.jsp" method="post" enctype="multipart/form-data"><label>文件:<input type="file" name="myFile"></label><br /> <label>其余信息:<input type="text" name="info" /></label><br /> <inputtype="submit" value="提交" /></form></body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.servlet.*" %><%@page import="org.apache.commons.fileupload.*" %><%@page import="org.apache.commons.fileupload.disk.*" %><%@page import="java.util.List" %><%@page import="java.util.Iterator" %><%@page import="java.io.*" %><%
//Check that we have a file upload request//监测request中是否包含文件boolean isMultipart = ServletFileUpload.isMultipartContent(request);if(isMultipart){out.print("包含文件");}else{out.print("不包含文件");}out.print("<br/>");// Create a factory for disk-based file itemsFileItemFactory factory = new DiskFileItemFactory();//获取文件须要上传到的路径// String path = request.getRealPath("/upload");String path = session.getServletContext().getRealPath("upload");//若是没如下两行设置的话,上传大的 文件 会占用 不少内存,//设置暂时存放的 存储室 , 这个存储室,能够和 最终存储文件 的目录不一样/*** 原理 它是先存到 暂时存储室,而后在真正写到 对应目录的硬盘上,* 按理来讲 当上传一个文件时,实际上是上传了两份,第一个是以 .tem 格式的* 而后再将其真正写到 对应目录的硬盘上*/// factory.setRepository(new File(path));//设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室// factory.setSizeThreshold(1024*1024) ;// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Parse the requestList items = upload.parseRequest(request);// Process the uploaded itemsIterator iter = items.iterator();while (iter.hasNext()) {FileItem item = (FileItem) iter.next();if (item.isFormField()) {//若是是普通表单控件////获取表单的属性名字String name = item.getFieldName();//获取用户具体输入的字符串 ,名字起得挺好,由于表单提交过来的是 字符串类型的String value = item.getString();request.setAttribute(name,value);out.print("name:"+name +"<br/>");out.print("value:"+value +"<br/>");} else {//若是是文件//也叫name是为了和上面保持一致,方便显示页面String name = item.getFieldName();String fileName = item.getName();String contentType = item.getContentType();boolean isInMemory = item.isInMemory();long sizeInBytes = item.getSize();out.print("表单文件控件名:"+name +"<br/>");//绝对路径的out.print("上传文件名:"+fileName +"<br/>");out.print("文件类型:"+contentType +"<br/>");out.print("是否保存在内存中:"+isInMemory +"<br/>");out.print("大小:"+sizeInBytes +"字节<br/>");//上传文件//获取文件名String f_name = fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());request.setAttribute(name,f_name);// 进行文件上传File uploadedFile = new File(path ,f_name);item.write(uploadedFile);/* OutputStream out = new FileOutputStream(new File(path,filename));InputStream in = item.getInputStream() ;int length = 0 ;byte [] buf = new byte[1024] ;System.out.println("获取上传文件的总共的容量:"+item.getSize());// in.read(buf) 每次读到的数据存放在 buf 数组中while( (length = in.read(buf) ) != -1){//在 buf 数组中 取出数据 写到 (输出流)磁盘上out.write(buf, 0, length);}in.close();*/}}request.getRequestDispatcher("showFile.jsp").forward(request, response);%>
若是有不足或者错误的地方,还请指出,不胜感激,愿你我共同进步。。。。<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>文件展现</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--
<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>用户名:${requestScope.usename } <br/>文件:${requestScope.myfile }<br/><!-- 把上传的图片显示出来 -->
<img alt="go" src="upload/<%=(String)request.getAttribute("myfile")%> " /></body></html>