001: package com.bostechcorp.cbesb.console.server;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.PrintWriter;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServlet;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import org.apache.commons.fileupload.FileItem;
015: import org.apache.commons.fileupload.FileItemFactory;
016: import org.apache.commons.fileupload.FileUploadException;
017: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
018: import org.apache.commons.fileupload.servlet.ServletFileUpload;
019: import org.apache.commons.io.FilenameUtils;
020:
021: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
022: import com.bostechcorp.cbesb.common.i18n.Messages;
023: import com.bostechcorp.cbesb.console.i18n.ConsoleMessages;
024: import com.google.gwt.core.client.GWT;
025:
026: public abstract class FileUploadServlet extends HttpServlet {
027: /**
028: *
029: */
030: private static final long serialVersionUID = 1L;
031:
032: protected abstract String getRootDir() throws Exception;
033:
034: protected void doPost(HttpServletRequest request,
035: HttpServletResponse response) throws ServletException,
036: IOException {
037:
038: String rootDirectory = "";
039: try {
040: // rootDirectory = ServerUtil.getComponentFolder();
041: rootDirectory = getRootDir();
042: } catch (Exception ex) {
043: throw new ServletException(ex.getMessage());
044: }
045: boolean writeToFile = true;
046: String returnOKMessage = Messages
047: .get(CoreMessageConstants.SUBMIT_COMPLETE);
048: String noChineseMessage = Messages
049: .get(CoreMessageConstants.LETTER_ONLY_PATH);
050: boolean isMultipart = ServletFileUpload
051: .isMultipartContent(request);
052:
053: PrintWriter out = response.getWriter();
054:
055: // Create a factory for disk-based file items
056:
057: if (isMultipart) {
058: // We are uploading a file (deletes are performed by on multipart
059: // requests)
060: FileItemFactory factory = new DiskFileItemFactory();
061:
062: // Create a new file upload handler
063: ServletFileUpload upload = new ServletFileUpload(factory);
064: // Parse the request
065: try {
066: List items = upload.parseRequest(request);
067:
068: if (items.size() == 0) {
069: try {
070:
071: out.print(noChineseMessage);
072: } catch (Exception e) {
073: e.printStackTrace();
074: out.print("Error:" + e.getMessage());
075: }
076: return;
077: }
078: // Process the uploaded items
079: Iterator iter = items.iterator();
080: while (iter.hasNext()) {
081: FileItem item = (FileItem) iter.next();
082:
083: if (item.isFormField()) {
084: } else {
085: if (writeToFile) {
086: String fileName = item.getName();
087: if (fileName != null
088: && !fileName.equals("")) {
089: fileName = FilenameUtils
090: .getName(fileName);
091: File uploadedFile = new File(
092: rootDirectory + fileName);
093: if (!uploadedFile.exists()) {
094: uploadedFile.createNewFile();
095: } else {
096: changeFilename(uploadedFile);
097: }
098: try {
099: item.write(uploadedFile);
100: out.print(returnOKMessage);
101: } catch (Exception e) {
102: e.printStackTrace();
103: out
104: .print("Error:"
105: + e.getMessage());
106: }
107: }
108: } else {
109: }
110: }
111: }
112: } catch (FileUploadException e) {
113: e.printStackTrace();
114: }
115: } else {
116: // Process a request to delete a file
117: String[] paramValues = request
118: .getParameterValues(((ConsoleMessages) GWT
119: .create(ConsoleMessages.class))
120: .uploadFormElement());
121: for (int i = 0; i < paramValues.length; i++) {
122: String fileName = FilenameUtils.getName(paramValues[i]);
123: File deleteFile = new File(rootDirectory + fileName);
124: if (deleteFile.delete()) {
125: out.print(returnOKMessage);
126: }
127: }
128: }
129:
130: }
131:
132: public abstract void changeFilename(File file);
133: }
|