文件上传实例记录
主要参照w3c实例:使用html格式做界面,使用jsp文件做后台处理。
页面代码:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploadFile.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
注意:action指定的是后台处理程序,负责文件上传的具体工作。传输方式采用post。entype的值一般如上所示,它和jsp文件中某个值相对应,我发现只要两个值相同就不会出错,但是用这个可能会更标准吧。
后台程序:
<%--
Created by IntelliJ IDEA.
User: GYL
Date: 2018/9/3
Time: 13:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%
File file ;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// String filePath="d:/GGG";
// 验证上传内容的类型
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存中存储文件的最大值
factory.setSizeThreshold(maxMemSize);
// 本地存储的数据大于 maxMemSize.
factory.setRepository(new File("d:\\"));
// 创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大上传的文件大小
upload.setSizeMax( maxFileSize );
try{
// 解析获取的文件
List fileItems = upload.parseRequest(request);
// 处理上传的文件
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>JSP File upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// 获取上传文件的参数
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// 写入文件
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath ,
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath ,
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + filePath +
fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}else{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
}
%>
注意:文件上传需要相应的包,具体可以去看w3c的教程。filePath设置文件存储路径,路径没啥特殊要求,只要路径正确就不会出错,不过也是安全为主。例子中的文件路径获取方式是读取web.xml中配置的值:
<context-param>
<param-name>file-upload</param-name>
<param-value>
d:\temp
</param-value>
当然也可以在jsp中直接写,就想我在下面注释的那一行,我试过了,正常运行。factory.setRepository(new File("d:\\temp"));
这个文件夹用于保存上传时的临时文件,一定要确保文件夹存在,至于它的路径和名字,你自己喜欢就好。String contentType = request.getContentType();
这个地方和html页面中的enctype对应,用来确定上传内容的类型,其实两个文件中的这个值只要对应就不会出错,不过还是用这个默认值吧!
if ((contentType.indexOf("multipart/form-data") >= 0))