我有一個應用程序,我嘗試從瀏覽器上傳Excel文件(約21,000行) - 應用程序使用JSP,Servlet,Apache POI和JDBC。 現在使用Servlet從瀏覽器(jsp文件)上傳文件,然後使用Apache POI讀取excel文件的記錄,然後將行寫入MySQL數據庫。上傳成功運行並能夠將記錄插入到Mysql數據庫中。但是,jsp頁面只停留在那裏,只有幾分鐘後纔會通知用戶成功上傳。將Excel上傳到Java Servlet並在數據上傳時得到結果
任何人都可以幫助我如何向用戶顯示瀏覽器上的消息,該文件已成功上傳,但正在處理? 任何更好的代碼實現也請讓我知道。 我的servlet代碼如下
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.json.simple.JSONObject;
import src.com.pion.web.config.business.UploadFileProcessor;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet(name = "UploadServlet",
description = "Manages the uploaded servlet file and sends it to processing",
urlPatterns = { "/upload.do" }
)
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(UploadServlet.class.getName());
private static final String UPLOAD_DIRECTORY = "upload";
private static final int THRESHOLD_SIZE = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
public UploadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throw new ServletException("GET method used with " +
getClass().getName()+": POST method required.");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//PrintWriter to send the JSON response back
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
PrintWriter writer = response.getWriter();
writer.println("Request does not contain upload data");
writer.flush();
return;
}
PrintWriter out = response.getWriter();
//set content type and header attributes
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
// configures upload settings
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(THRESHOLD_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
// constructs the directory path to store upload file
String uploadPath = getServletContext().getRealPath("")
+ File.separator + UPLOAD_DIRECTORY;
// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
JSONObject myObj = new JSONObject();
String fileName = null;
String fullName = null;
File file = null;
try {
// parses the request's content to extract file data
List formItems = upload.parseRequest(request);
Iterator iter = formItems.iterator();
// iterates over form's fields
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
file = new File(filePath);
fullName = item.getName().trim();
// saves the file on disk
item.write(file);
}
} String message = null;
UploadFileProcessor uploadProcessor = new UploadFileProcessor();
String extension = FilenameUtils.getExtension(fullName);
if(extenter code hereension.trim().equalsIgnoreCase("xlsx")){
// Send the code for processing and later insert into database
message = uploadProces`enter code `enter code here`here`sor.processExcelFile(file);
}
myObj.put("success", true);
myObj.put("message", message + " item(s) were processed for file " + fileName);
LOGGER.info(myObj.toString());
}
catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
myObj.put("success", false);
LOGGER.info(myObj.toString());
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
myObj.put("success", false);
LOGGER.info(myObj.toString());
}
out.close();
}
}