Gwt SampleUploadServlet,如何将文件保存到服务器(JAVA,GWT)中的文件夹中?

时间:2014-04-04 02:42:20

标签: java gwt

我从互联网复制了此代码(允许用户从客户端上传图像) https://code.google.com/p/gwtupload/source/browse/samples/src/main/java/gwtuploadsample/server/SampleUploadServlet.java?r=e0b4acbaa632114978b085801a004116b6dbcf52

但是它没有给出将文件保存到服务器

中的文件夹的实际代码
package my.server;
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());

          //Window.alert(file.getAbsolutePath()+" oook");
          //String s=file.getAbsolutePath()+" oook";
          /// Send a customized message to the client.
          response += "File saved as " + file.getAbsolutePath();
          //FileWriter fstream = new FileWriter(file.getAbsolutePath()+"//folder//out.txt",true);
          //SEEM WE NEED TO DO SOMETHING HERE TO SAVE A FILE INTO THE FOLDER.
        } catch (Exception e) {
          throw new UploadActionException(e);
        }
      }
    }

   // System.out.println(response);
    /// Remove files from session because we have a copy of them
    removeSessionFileItems(request);

    /// Send your customized message to the client.
    return response;
  }

  /**
   * Get the content of an uploaded file.
   */
  @Override
  public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String fieldName = request.getParameter(UConsts.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, XML_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();
    }
  }
}

假设我在包“image”中有一个文件夹server, 那么如何将文件保存到server/image

我认为只有Java知识才能做到这一点,你不需要Gwt知识。

1 个答案:

答案 0 :(得分:1)

这不是我的解决方案。有人已经回复了here

你应该使用File#createTempFile()代替目录。

File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
item.write(file);

或者如果你真的想在之后将临时文件移动到另一个位置,请使用File#renameTo()。

File destination = new File("/path/to/your/uploads", file.getName());
file.renameTo(destination);