104 lines
3.8 KiB
Plaintext
104 lines
3.8 KiB
Plaintext
package th.co.muangthai.endrprint;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
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 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;
|
|
|
|
/**
|
|
* Servlet implementation class UploadServlet
|
|
*/
|
|
public class UploadServlet extends HttpServlet {
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static final String DATA_DIRECTORY = "data";
|
|
private static final int MAX_MEMORY_SIZE = 1024 * 1024 * 2;
|
|
private static final int MAX_REQUEST_SIZE = 1024 * 1024;
|
|
|
|
public static String currentUploadPath = "Bah";
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
// Check that we have a file upload request
|
|
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
|
|
String dir = currentUploadPath;
|
|
|
|
if (!isMultipart) {
|
|
return;
|
|
}
|
|
|
|
// Create a factory for disk-based file items
|
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
|
|
|
// Sets the size threshold beyond which files are written directly to
|
|
// disk.
|
|
factory.setSizeThreshold(MAX_MEMORY_SIZE);
|
|
|
|
// Sets the directory used to temporarily store files that are larger
|
|
// than the configured size threshold. We use temporary directory for
|
|
// java
|
|
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
|
|
|
|
// constructs the folder where uploaded file will be stored
|
|
// String uploadFolder = getServletContext().getRealPath("")
|
|
// + File.separator + DATA_DIRECTORY;
|
|
|
|
String uploadFolder = dir;
|
|
|
|
// Create a new file upload handler
|
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
|
|
|
// Set overall request size constraint
|
|
upload.setSizeMax(MAX_REQUEST_SIZE);
|
|
|
|
try {
|
|
// Parse the request
|
|
List items = upload.parseRequest(request);
|
|
Iterator iter = items.iterator();
|
|
while (iter.hasNext()) {
|
|
FileItem item = (FileItem) iter.next();
|
|
|
|
if (!item.isFormField()) {
|
|
String fileName = new File(item.getName()).getName();
|
|
String filePath = uploadFolder + File.separator +fileName;
|
|
File uploadedFile = new File(filePath);
|
|
System.out.println(filePath);
|
|
// System.out.println(getServletContext().getRealPath("/"));uploadFolder +
|
|
// saves the file to upload directory
|
|
item.write(uploadedFile);
|
|
}
|
|
}
|
|
|
|
// displays done.jsp page after upload finished
|
|
//?pathTo="+(VSMUtil.isEmpty(currentUploadPath)?"Bah":currentUploadPath)
|
|
// request.setAttribute("currentPathTo",currentUploadPath);
|
|
getServletContext().getRequestDispatcher("/endrpathfile.jsp").forward(
|
|
request, response);
|
|
|
|
} catch (FileUploadException ex) {
|
|
throw new ServletException(ex);
|
|
} catch (Exception ex) {
|
|
throw new ServletException(ex);
|
|
}
|
|
|
|
}
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
currentUploadPath = request.getParameter("pathTo");
|
|
request.setAttribute("currentPathTo", currentUploadPath);
|
|
getServletContext().getRequestDispatcher("/endrpathfile.jsp").forward(
|
|
request, response);
|
|
|
|
}
|
|
|
|
} |