Servlet3.0上傳圖片示例
一、前端JSP頁面
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet3.0實現文件上傳</title>
</head>
<body>
<h1>Servlet3.0實現文件上傳</h1>
<b id="msg" style="color: red;"></b>
<!-- 表單提交多媒體(此處指圖片)必須要加enctype="multipart/form-data"屬性和值 -->
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password" /></td>
<tr>
<td>頭像:</td>
<td><input type="file" name="photo" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
var fm = document.forms[0];
//var form = document.myform;//不能有重名的,否則不能獲取到
var msg = document.getElementById("msg");
function doCheck(fm) {
if (null == fm.username.value || "" == fm.username.value) {
msg.innerHTML = "* 用戶名不能為空!";
fm.username.focus();
return false;
}
if (null == fm.password.value || "" == fm.password.value) {
msg.innerHTML = "* 密碼不能為空!";
fm.password.focus();
return false;
}
if (null == fm.photo.value || "" == fm.photo.value) {
msg.innerHTML = "* 上傳的文件不能為空!";
return false;
}
}
</script>
</body>
</html>
二、後臺邏輯處理的Servlet,需要導入文件上傳處理包:commons-io -2.4.jar、commons -fileupload -1.3.1.jar
package com.qubo.servlet3x;
import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
@WebServlet("/fileUpload1")
@MultipartConfig
public class FileUploadServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Servlet3.0中,沒有提供直接獲取上傳文件的文件名方法。需要使用如下方式來獲取文件名
*/
protected String getFileName(Part part) {
String fileName = null;
String cotentDesc = part.getHeader("Content-Disposition");
Pattern pattern = Pattern.compile("filename=\"(.+)\"");
Matcher matcher = pattern.matcher(cotentDesc);
if (matcher.find()) {
fileName = matcher.group(1);
}
return fileName;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("username");
String password = request.getParameter("password");
Part part = request.getPart("photo");
String fileName = this.getFileName(part); //調用獲取文件名的方法
String str = part.getName(); //前端jsp頁面中指定的name屬性的值,即<input type="file" name="photo" />中的photo
System.out.println("用戶名:" + name + ",密碼:" + password + ",文件名:" + fileName + ",圖像輸入框名:" + str);
String hzm = fileName.substring(fileName.indexOf("."));//獲取文件的後綴名
part.write("E:/ZhuoXun JavaWeb/Servlet3.x/WebContent/img/" + new Date().getTime() + hzm);//文件的保存位置
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Servlet3.0上傳圖片示例