JAVA实现文件上传
代码如下:
还要两个jar包
前台页面:
01 |
<%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> |
03 |
String path = request.getContextPath(); |
04 |
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
10 |
<base href= "<%=basePath%>" > |
12 |
<title>My JSP 'index.jsp' starting page</title> |
13 |
<meta http-equiv= "pragma" content= "no-cache" > |
14 |
<meta http-equiv= "cache-control" content= "no-cache" > |
15 |
<meta http-equiv= "expires" content= "0" > |
16 |
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > |
17 |
<meta http-equiv= "description" content= "This is my page" > |
19 |
<link rel= "stylesheet" type= "text/css" href= "styles.css" > |
26 |
enctype:multipart/form-data 以二进制的形式向服务器传递参数 |
28 |
<form action= "fileUp.do" method= "post" enctype= "multipart/form-data" > |
32 |
<td><input type= "file" name= "file" ></td> |
35 |
<td><input type= "submit" value= "开始上传" ></td> |
后端代码:
04 |
import java.io.IOException; |
05 |
import java.io.PrintWriter; |
06 |
import java.util.Iterator; |
07 |
import java.util. List ; |
09 |
import javax.servlet.http.HttpServlet; |
10 |
import javax.servlet.http.HttpServletRequest; |
11 |
import javax.servlet.http.HttpServletResponse; |
13 |
import org.apache.commons.fileupload.FileItem; |
14 |
import org.apache.commons.fileupload.FileUploadException; |
15 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
16 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
18 |
public class FileUpServlet extends HttpServlet{ |
20 |
public void service(HttpServletRequest request,HttpServletResponse response) |
22 |
request.setCharacterEncoding( "UTF-8" ); |
23 |
response.setCharacterEncoding( "GBK" ); |
25 |
DiskFileItemFactory factory = new DiskFileItemFactory(); |
27 |
ServletFileUpload upload = new ServletFileUpload(factory); |
30 |
List items = upload.parseRequest(request); |
31 |
Iterator itr = items.iterator(); |
32 |
while (itr.hasNext()) { |
33 |
FileItem fileItem = (FileItem)itr.next(); |
34 |
if (!fileItem.isFormField()){ |
35 |
long fileSize = fileItem.getSize(); |
36 |
String fileName = fileItem.getName(); |
38 |
File tempFile = new File(fileName); |
40 |
String updatePath = this .getServletContext().getRealPath( "/upload" ); |
42 |
File file = new File(updatePath,tempFile.getName()); |
45 |
PrintWriter out = response.getWriter(); |
46 |
out.print( "文件上传成功! " ); |
47 |
out.print( "上传路径为: " +updatePath); |
48 |
out.print( "文件名: " +fileName); |
49 |
out.print( "文件的大小为: " +fileSize+ " KB" ); |
52 |
} catch (FileUploadException e) { |
54 |
} catch (Exception e) { |
web.xml:
01 |
<!--?xml version= "1.0" encoding= "UTF-8" ?--> |
03 |
<display-name>FileUp</display-name> |
05 |
<welcome-file>index.html</welcome-file> |
06 |
<welcome-file>index.htm</welcome-file> |
07 |
<welcome-file>index.jsp</welcome-file> |
08 |
<welcome-file> default .html</welcome-file> |
09 |
<welcome-file> default .htm</welcome-file> |
10 |
<welcome-file> default .jsp</welcome-file> |
15 |
<servlet-name>file</servlet-name> |
16 |
<servlet- class >com.zengda.FileUpServlet</servlet- class > |
21 |
<servlet-name>file</servlet-name> |
22 |
<url-pattern>/fileUp. do </url-pattern> |
原文地址:https://www.cnblogs.com/zengda/p/4297779.html