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"属性和值 -->
<form name="myform" action="fileUpload1" method="post" enctype="multipart/form-data" onSubmit="return doCheck(this);">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<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.forms['myform'];
//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);
}
}