|
|
|
|
@ -29,16 +29,25 @@ public class UploadServlet extends HttpServlet {
|
|
|
|
|
* @throws ServletException 如果处理请求时发生错误。
|
|
|
|
|
* @throws IOException 如果输入或输出异常发生。
|
|
|
|
|
*/
|
|
|
|
|
// 处理POST请求
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// 设置请求编码为utf-8
|
|
|
|
|
request.setCharacterEncoding("utf-8");
|
|
|
|
|
// 设置响应编码为utf-8
|
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
|
// 设置响应内容类型为text/html,编码为utf-8
|
|
|
|
|
response.setContentType("text/html; charset=utf-8");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断请求是否为文件上传
|
|
|
|
|
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
|
|
|
|
|
// 如果是文件上传
|
|
|
|
|
if (isMultipart) {
|
|
|
|
|
// 创建一个文件项工厂
|
|
|
|
|
FileItemFactory factory = new DiskFileItemFactory();
|
|
|
|
|
// 创建一个文件上传对象
|
|
|
|
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
|
|
|
|
// 设置文件上传对象的编码为utf-8
|
|
|
|
|
upload.setHeaderEncoding("utf-8");
|
|
|
|
|
|
|
|
|
|
// upload.setProgressListener(new ProgressListener(){
|
|
|
|
|
@ -54,11 +63,17 @@ public class UploadServlet extends HttpServlet {
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 解析请求,获取上传的文件项
|
|
|
|
|
List<FileItem> items = upload.parseRequest(request);
|
|
|
|
|
// 获取文件项的迭代器
|
|
|
|
|
Iterator<FileItem> it = items.iterator();
|
|
|
|
|
// 遍历文件项
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
// 获取下一个文件项
|
|
|
|
|
FileItem item = it.next();
|
|
|
|
|
// 获取文件项的名称
|
|
|
|
|
String itemname = item.getFieldName();
|
|
|
|
|
// 初始化学号和姓名
|
|
|
|
|
int sno = -1;
|
|
|
|
|
String sname = null;
|
|
|
|
|
|
|
|
|
|
@ -74,21 +89,31 @@ public class UploadServlet extends HttpServlet {
|
|
|
|
|
} else {
|
|
|
|
|
String filename = item.getName();
|
|
|
|
|
//String path=request.getSession().getServletContext().getRealPath("upload");
|
|
|
|
|
// 获取上传文件的路径
|
|
|
|
|
String path = this.getServletContext().getRealPath("upload");
|
|
|
|
|
// 判断上传文件的扩展名是否为jsp、htm或html
|
|
|
|
|
if (filename.substring(filename.lastIndexOf(".") + 1).equals("jsp")
|
|
|
|
|
|| filename.substring(filename.lastIndexOf(".") + 1).equals("htm")
|
|
|
|
|
|| filename.substring(filename.lastIndexOf(".") + 1).equals("html")) {
|
|
|
|
|
// 如果是,则转发到错误页面
|
|
|
|
|
request.getRequestDispatcher("error.jsp").forward(request, response);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果不是,则打印上传文件的路径
|
|
|
|
|
System.out.println(path);
|
|
|
|
|
// 创建文件对象
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
// 如果文件不存在且不是目录,则创建目录
|
|
|
|
|
if (!file.exists() && !file.isDirectory()) {
|
|
|
|
|
file.mkdir();
|
|
|
|
|
}
|
|
|
|
|
// 将上传的文件写入到指定路径
|
|
|
|
|
item.write(new File(path, filename));
|
|
|
|
|
// 设置上传成功的属性
|
|
|
|
|
request.setAttribute("news", filename + " 上传成功!");
|
|
|
|
|
// 转发到上传成功页面
|
|
|
|
|
request.getRequestDispatcher("/WEB-INF/admin/uploadFile.jsp").forward(request, response);
|
|
|
|
|
// response.sendRedirect("fileUploadIndexServlet");
|
|
|
|
|
// 打印上传成功的消息
|
|
|
|
|
System.out.println(filename + "上传成功!!!");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@ -107,11 +132,17 @@ public class UploadServlet extends HttpServlet {
|
|
|
|
|
/**
|
|
|
|
|
* 表示文件上传或下载过程中的信息类
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个ProcessInfo类,用于存储进程信息
|
|
|
|
|
class ProcessInfo{
|
|
|
|
|
// 总大小
|
|
|
|
|
public long totalSize = 1;
|
|
|
|
|
// 已读大小
|
|
|
|
|
public long readSize = 0;
|
|
|
|
|
// 显示信息
|
|
|
|
|
public String show = "";
|
|
|
|
|
// 项目数量
|
|
|
|
|
public int itemNum = 0;
|
|
|
|
|
// 速率
|
|
|
|
|
public int rate = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -119,3 +150,155 @@ public class UploadServlet extends HttpServlet {
|
|
|
|
|
doPost(request, response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//package web.servlet.file;
|
|
|
|
|
//
|
|
|
|
|
//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 java.io.IOException;
|
|
|
|
|
//import java.io.File;
|
|
|
|
|
//import java.util.Iterator;
|
|
|
|
|
//import java.util.List;
|
|
|
|
|
//import org.apache.commons.fileupload.FileItem;
|
|
|
|
|
//import org.apache.commons.fileupload.FileItemFactory;
|
|
|
|
|
//import org.apache.commons.fileupload.FileUploadException;
|
|
|
|
|
//import org.apache.commons.fileupload.ProgressListener;
|
|
|
|
|
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
|
|
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
|
|
///**
|
|
|
|
|
// * 文件上传的Servlet
|
|
|
|
|
// */
|
|
|
|
|
//@WebServlet("/uploadServlet")
|
|
|
|
|
//public class UploadServlet extends HttpServlet {
|
|
|
|
|
//
|
|
|
|
|
// /**
|
|
|
|
|
// * 处理POST请求,用于执行文件上传操作。
|
|
|
|
|
// *
|
|
|
|
|
// * @param request HTTP请求对象,包含客户端发送的数据。
|
|
|
|
|
// * @param response HTTP响应对象,用于生成返回给客户端的响应。
|
|
|
|
|
// * @throws ServletException 如果处理请求时发生错误。
|
|
|
|
|
// * @throws IOException 如果输入或输出异常发生。
|
|
|
|
|
// */
|
|
|
|
|
// // 处理POST请求
|
|
|
|
|
// protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// // 设置请求编码为utf-8
|
|
|
|
|
// request.setCharacterEncoding("utf-8");
|
|
|
|
|
// // 设置响应编码为utf-8
|
|
|
|
|
// response.setCharacterEncoding("utf-8");
|
|
|
|
|
// // 设置响应内容类型为text/html,编码为utf-8
|
|
|
|
|
// response.setContentType("text/html; charset=utf-8");
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// // 判断请求是否为文件上传
|
|
|
|
|
// boolean isMultipart=ServletFileUpload.isMultipartContent(request);
|
|
|
|
|
// // 如果是文件上传
|
|
|
|
|
// if (isMultipart) {
|
|
|
|
|
// // 创建一个文件项工厂
|
|
|
|
|
// FileItemFactory factory = new DiskFileItemFactory();
|
|
|
|
|
// // 创建一个文件上传对象
|
|
|
|
|
// ServletFileUpload upload = new ServletFileUpload(factory);
|
|
|
|
|
// // 设置文件上传对象的编码为utf-8
|
|
|
|
|
// upload.setHeaderEncoding("utf-8");
|
|
|
|
|
//
|
|
|
|
|
//// upload.setProgressListener(new ProgressListener(){
|
|
|
|
|
//// public void update(long pBytesRead, long pContentLength, int pItems) {
|
|
|
|
|
//// ProcessInfo pri = new ProcessInfo();
|
|
|
|
|
//// pri.itemNum = pItems;
|
|
|
|
|
//// pri.readSize = pBytesRead;
|
|
|
|
|
//// pri.totalSize = pContentLength;
|
|
|
|
|
//// pri.show = pBytesRead+"/"+pContentLength+" byte";
|
|
|
|
|
//// pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength)*100);
|
|
|
|
|
//// hs.setAttribute("proInfo", pri);
|
|
|
|
|
//// }
|
|
|
|
|
//// });
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// // 解析请求,获取上传的文件项
|
|
|
|
|
// List<FileItem> items = upload.parseRequest(request);
|
|
|
|
|
// // 获取文件项的迭代器
|
|
|
|
|
// Iterator<FileItem> it = items.iterator();
|
|
|
|
|
// // 遍历文件项
|
|
|
|
|
// while (it.hasNext()) {
|
|
|
|
|
// // 获取下一个文件项
|
|
|
|
|
// FileItem item = it.next();
|
|
|
|
|
// // 获取文件项的名称
|
|
|
|
|
// String itemname = item.getFieldName();
|
|
|
|
|
// // 初始化学号和姓名
|
|
|
|
|
// int sno = -1;
|
|
|
|
|
// String sname = null;
|
|
|
|
|
//
|
|
|
|
|
// if (item.isFormField()) {
|
|
|
|
|
// if (itemname.equals("sno")) {
|
|
|
|
|
// sno = Integer.parseInt(item.getString("utf-8"));
|
|
|
|
|
// } else if (itemname.equals("sname")) {
|
|
|
|
|
// sname = item.getString("utf-8");
|
|
|
|
|
// sname = item.getName();
|
|
|
|
|
// } else {
|
|
|
|
|
// System.out.println("其他字段");
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// String filename = item.getName();
|
|
|
|
|
// //String path=request.getSession().getServletContext().getRealPath("upload");
|
|
|
|
|
// // 获取上传文件的路径
|
|
|
|
|
// String path = this.getServletContext().getRealPath("upload");
|
|
|
|
|
// // 判断上传文件的扩展名是否为jsp、htm或html
|
|
|
|
|
// if (filename.substring(filename.lastIndexOf(".") + 1).equals("jsp")
|
|
|
|
|
// || filename.substring(filename.lastIndexOf(".") + 1).equals("htm")
|
|
|
|
|
// || filename.substring(filename.lastIndexOf(".") + 1).equals("html")) {
|
|
|
|
|
// // 如果是,则转发到错误页面
|
|
|
|
|
// request.getRequestDispatcher("error.jsp").forward(request, response);
|
|
|
|
|
// } else {
|
|
|
|
|
// // 如果不是,则打印上传文件的路径
|
|
|
|
|
// System.out.println(path);
|
|
|
|
|
// // 创建文件对象
|
|
|
|
|
// File file = new File(path);
|
|
|
|
|
// // 如果文件不存在且不是目录,则创建目录
|
|
|
|
|
// if (!file.exists() && !file.isDirectory()) {
|
|
|
|
|
// file.mkdir();
|
|
|
|
|
// }
|
|
|
|
|
// // 将上传的文件写入到指定路径
|
|
|
|
|
// item.write(new File(path, filename));
|
|
|
|
|
// // 设置上传成功的属性
|
|
|
|
|
// request.setAttribute("news", filename + " 上传成功!");
|
|
|
|
|
// // 转发到上传成功页面
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/admin/uploadFile.jsp").forward(request, response);
|
|
|
|
|
//// response.sendRedirect("fileUploadIndexServlet");
|
|
|
|
|
// // 打印上传成功的消息
|
|
|
|
|
// System.out.println(filename + "上传成功!!!");
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// } catch (FileUploadException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// /**
|
|
|
|
|
// * 表示文件上传或下载过程中的信息类
|
|
|
|
|
// */
|
|
|
|
|
// // 定义一个ProcessInfo类,用于存储进程信息
|
|
|
|
|
// class ProcessInfo{
|
|
|
|
|
// // 总大小
|
|
|
|
|
// public long totalSize = 1;
|
|
|
|
|
// // 已读大小
|
|
|
|
|
// public long readSize = 0;
|
|
|
|
|
// // 显示信息
|
|
|
|
|
// public String show = "";
|
|
|
|
|
// // 项目数量
|
|
|
|
|
// public int itemNum = 0;
|
|
|
|
|
// // 速率
|
|
|
|
|
// public int rate = 0;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// doPost(request, response);
|
|
|
|
|
// }
|
|
|
|
|
//}
|