文件上传

文件上传要素:

1
<form action="提交地址" method="post" enctype="multipart/form-data">
1
<input type="file" name="pic">  <!--name值随便写>

后台Servlet上必须加上注解 @MultipartConfig

代码实现:

UploadListener类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.fkjava.listener;

import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class UploadListener implements ServletContextListener {


public UploadListener() {
// TODO Auto-generated constructor stub
}

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}

public void contextInitialized(ServletContextEvent event) {
// 初始化创建一个上传的文件夹,如果文件夹存在就不再重复创建

// 获得ServletContext
ServletContext context = event.getServletContext();

// 获得web项目的根路径下的upload的路径
String realPath = context.getRealPath("/upload");
System.out.println("realPath:"+realPath);
// 构造一个File
File file = new File(realPath);
// 判断该File是否存在
if(!file.exists()){
// 如果不存在,创建
file.mkdirs();
System.out.println("创建上传文件夹成功!");
}else{
System.out.println("上传文件夹已经存在!");
}
}

}

UploadServlet类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.fkjava.servlet;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
* 文件上传的要素
* 1、页面中请求方式必须是 post请求
* 2、form表单必须加上属性 enctype="multipart/form-data"
* 3、后台Servlet上必须加上注解 @MultipartConfig
*/
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}

protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

String username = request.getParameter("username");
System.out.println("username:" + username);

Part part = request.getPart("pic");
/*常用方法*/
System.out.println("name:" + part.getName());
System.out.println("size:" + part.getSize());
System.out.println("SubmittedFileName:" + part.getSubmittedFileName());
System.out.println("ContentType:" + part.getContentType());

// 获得上传的文件的原来的名称 1.jpg == > 434343fdfdffd.jpg
String fileName = part.getSubmittedFileName();

//通过UUID生成随机数
String fn = UUID.randomUUID().toString();

// 获得上传的文件夹路径
String filePath = this.getServletContext().getRealPath("/upload");
System.out.println("filePath:"+filePath);

// 上传 File.separator 用于获取文件分隔符 d:upload/4343434334343.jpg
part.write(filePath + File.separator + fn+fileName.substring(fileName.lastIndexOf("."), fileName.length()));
System.out.println("上传文件成功!");
}

}

index.jsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action='fileUpload' method= 'post' enctype='multipart/form-data'>
选择文件:<input type="file" name='pic' /><br>
<input type="submit" value="提交">
</form>

</body>
</html>

-------------本文结束感谢您的阅读-------------