📄 uploadservlet.java
字号:
package com.ghy.bookstore.load.web;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.ghy.bookstore.load.dao.FileLinkDao;
import com.ghy.bookstore.load.model.FileLinkVO;
import com.ghy.bookstore.user.model.UserVO;
public class UploadServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UploadServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
List fileList = null;
final long MAX_SIZE = 3 * 1024 * 1024;
final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "txt",
"doc", "docx", "mp3", "wma", "m4a" };
try {
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(4096);
dfif.setRepository(new File(request.getRealPath("/")
+ "ImagesUploadTemp"));
ServletFileUpload sfu = new ServletFileUpload(dfif);
sfu.setSizeMax(MAX_SIZE);
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {
if (e instanceof SizeLimitExceededException) {
String message = "文件尺寸超过规定大小" + MAX_SIZE / (1024 * 1024)
+ "M大小";
request.getRequestDispatcher(
"/jsp/load/upload.jsp?message="
+ URLEncoder.encode(message, "utf-8")).forward(
request, response);
return;
}
e.printStackTrace();
}
if (fileList == null || fileList.size() == 0) {
String message = "请选择上传文件";
request.getRequestDispatcher(
"/jsp/load/upload.jsp?message="
+ URLEncoder.encode(message, "utf-8")).forward(
request, response);
return;
}
Iterator fileItr = fileList.iterator();
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
fileItem = (FileItem) fileItr.next();
if (fileItem == null || fileItem.isFormField()) {
continue;
}
path = fileItem.getName();
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
String message = "请选择上传文件";
request.getRequestDispatcher(
"/jsp/load/upload.jsp?message="
+ URLEncoder.encode(message, "utf-8")).forward(
request, response);
return;
}
String t_name = path.substring(path.lastIndexOf("\\") + 1);
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
String message = "请上传以下类型文件";
request.getRequestDispatcher(
"/jsp/load/upload.jsp?message="
+ URLEncoder.encode(message, "utf-8")).forward(
request, response);
return;
}
try {
String u_name = request.getRealPath("/") + "imagesUploadTemp/"
+ t_name;
fileItem.write(new File(u_name));
// 将上传文件信息存储到filelink表
FileLinkVO filelink = new FileLinkVO();
HttpSession session = request.getSession();
UserVO user = (UserVO) session.getAttribute("CurrentUser");
filelink.setUserid(user.getId());
filelink.setFilename(t_name);
filelink.setFilelink(u_name);
new FileLinkDao().setFileLink(filelink);
// 读取filelink表信息
ArrayList list = new ArrayList();
list = new FileLinkDao().getFileLink();
session.setAttribute("fileLink", list);
//重定向到upload.jsp
String message = "文件上传成功:保存目录为:" + request.getRealPath("/")
+ "ImagesUploadTemp" + "文件名为: " + t_name
+ " 文件大小: " + size + "字节";
request.getRequestDispatcher(
"/jsp/load/upload.jsp?message="
+ URLEncoder.encode(message, "utf-8")).forward(
request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -