文件上传用到jspSmartUpload组建,它可以非常方便地完成这个工作。
【坑点1:】
下载好的jar包,要放在webroot/web-inf/lib 目录下。否则服务器启动错误的。
【基本流程】:
用户在首页选择本机文件上传,执行上传时是调用一个Servlet,在该Servlet中我们编写核心上传代码,最后跳转到一个jsp页面,展示用户上传成功的图片。
先写个上传首页:
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
</head>
<body>
<h2>欢迎来到首页</h2>
<form action="/TestConnectionPool/CenterServlet" method="post" enctype="multipart/form-data">
<input type="file" value="file" name="file"/>
<input type="submit" value="提交" />
</form>
</body>
</html>
大致上是这个效果:
这里上传文件表单直接用type=file。
注意这里表单的 enctype="multipart/form-data"要这样写,因为我们这里涉及到图片等文件的传输,这个属性的意思就是表单上传二进制数据。
第二步就是核心部分了,编写Servlet。
Servlet代码:
package controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import enitity.Student;
import model.StudentDao;
/**
* Servlet implementation class CenterServlet
*/
@WebServlet("/CenterServlet")
public class CenterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CenterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SmartUpload smu = new SmartUpload();
//初始化smu对象
smu.initialize(getServletConfig(), request, response);
//设置可以上传的文件类型
smu.setAllowedFilesList("gif,jpg,doc,txt");
try {
//设置不能上传的文件类型
smu.setDeniedFilesList("exe,bat");
//设置单个文件最大限制
smu.setMaxFileSize(1000000);
//设置总共上传文件限制
smu.setTotalMaxFileSize(20000000);
//执行上传
smu.upload();
smu.setContentDisposition("utf-8");
//得到单个上传文件的信息
com.jspsmart.upload.File file = null;
file = smu.getFiles().getFile(0);
String filePath = null;
if(!file.isMissing()) {
//设置文件在服务器的保存位置
filePath = "upload\\";
String fileName = file.getFileName();
//对文件名进行这样的处理,就不会有中文乱码了
fileName = new String(fileName.getBytes("GBK"), "utf-8");
filePath += fileName;
file.saveAs(filePath, SmartUpload.SAVE_VIRTUAL);
}
//surequest对象用来获取表单提交的照片名
//com.jspsmart.upload.Request surequest = smu.getRequest();
//String name = surequest.getParameter("name");
//姓名存放到request供jsp展示
//request.setAttribute("name", name);
request.setAttribute("photofilepath", filePath);
//跳转到展示界面
request.getRequestDispatcher("showphoto.jsp").forward(request, response);
} catch (SQLException e) {
System.out.println("dopost方法出现异常");
e.printStackTrace();
} catch (SmartUploadException e) {
System.out.println("upload方法异常");
e.printStackTrace();
}
}
}
最后的显示页面:
<%@ page import="enitity.*" language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>展示界面</title>
</head>
<body>
<h1>你的照片</h1>
<img src=${requestScope.photofilepath } />
照片路径:${requestScope.photofilepath }
</body>
</html>
最终图片应该是上传到了服务器的
C:\Users\asus\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\项目名\upload目录下面