1

I am using this gwt upload system here(http://code.google.com/p/gwtupload/). I am getting some problems with it.

  1. Show to feed it with a path from the client
  2. Get the path on the server where the file was saved
  3. set a path on the server where the file is to be saved

This the servlet to handle the file upload

public class SampleUploadServlet extends UploadAction {

  private static final long serialVersionUID = 1L;

  Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
  /**
   * Maintain a list with received files and their content types. 
   */
  Hashtable<String, File> receivedFiles = new Hashtable<String, File>();

  /**
   * Override executeAction to save the received files in a custom place
   * and delete this items from session.  
   */
  @Override
  public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
    String response = "";
    int cont = 0;
    for (FileItem item : sessionFiles) {
      if (false == item.isFormField()) {
        cont ++;
        try {
          /// Create a new file based on the remote file name in the client
          // String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
          // File file =new File("/tmp/" + saveName);

          /// Create a temporary file placed in /tmp (only works in unix)
          // File file = File.createTempFile("upload-", ".bin", new File("/tmp"));

          /// Create a temporary file placed in the default system temp folder
          File file = File.createTempFile("upload-", ".bin");
          item.write(file);

          /// Save a list with the received files
          receivedFiles.put(item.getFieldName(), file);
          receivedContentTypes.put(item.getFieldName(), item.getContentType());

          /// Compose a xml message with the full file information which can be parsed in client side
          response += "<file-" + cont + "-field>" + item.getFieldName() + "</file-" + cont + "-field>\n";
          response += "<file-" + cont + "-name>" + item.getName() + "</file-" + cont + "-name>\n";
          response += "<file-" + cont + "-size>" + item.getSize() + "</file-" + cont + "-size>\n";
          response += "<file-" + cont + "-type>" + item.getContentType()+ "</file-" + cont + "type>\n";
        } catch (Exception e) {
          throw new UploadActionException(e);
        }
      }
    }

    /// Remove files from session because we have a copy of them
    removeSessionFileItems(request);

    /// Send information of the received files to the client.
    return "<response>\n" + response + "</response>\n";
  }

  /**
   * Get the content of an uploaded file.
   */
  @Override
  public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String fieldName = request.getParameter(PARAM_SHOW);
    File f = receivedFiles.get(fieldName);
    if (f != null) {
      response.setContentType(receivedContentTypes.get(fieldName));
      FileInputStream is = new FileInputStream(f);
      copyFromInputStreamToOutputStream(is, response.getOutputStream());
    } else {
      renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND);
   }
  }

  /**
   * Remove a file when the user sends a delete request.
   */
  @Override
  public void removeItem(HttpServletRequest request, String fieldName)  throws UploadActionException {
    File file = receivedFiles.get(fieldName);
    receivedFiles.remove(fieldName);
    receivedContentTypes.remove(fieldName);
    if (file != null) {
      file.delete();
    }
  }
}

Thanks

kroiz
  • 1,722
  • 1
  • 27
  • 43
Noor
  • 19,638
  • 38
  • 136
  • 254

1 Answers1

0

Try with this:

public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
for (FileItem item : sessionFiles) {
    if (false == item.isFormField()) {
        String uploadedFileName = "";
        try {
            String uploadsDir = "/uploads";
            File dirFile = new File(uploadsDir);
                dirFile.mkdirs();

                String filename = FilenameUtils.getName(item.getName()); // uploaded file filename

            File file = new File(uploadsDir, filename);
            item.write(file);

            uploadedFileName = uploadsDir + "/" + filename;
        } catch (Exception e) {
            logger.error("ERROR UPLOADING FILE: " + uploadedFileName +  ", Exception: " + e);
            throw new UploadActionException(e.getMessage());
        }
    }
removeSessionFileItems(request);

} return null; }

Happy coding!

Regards.

iVela
  • 1,160
  • 1
  • 19
  • 40
  • This code is not that different from the OP's code. you just save the file to a different location. I see no handling on how to communicate the location of the file on the server - when the client will get the url it will be the url in the session not the files system. – kroiz May 21 '12 at 06:44