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

Gwt SampleUploadServlet,如何将文件保存到服务器(JAVA,Gwt)中的文件夹中?,java,gwt,Java,Gwt,我从互联网上复制了这段代码(允许用户将图像从客户端上传到服务器) 但它并没有给出将文件保存到服务器文件夹中的实际代码 package my.server; public class SampleUploadServlet extends UploadAction{ private static final long serialVersionUID = 1L; Hashtable<String, String> receivedContentTypes = new Has

我从互联网上复制了这段代码(允许用户将图像从客户端上传到服务器)

但它并没有给出将文件保存到服务器文件夹中的实际代码

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();
    }
  }
}
打包my.server;
公共类SampleUploadServlet扩展了UploadAction{
私有静态最终长serialVersionUID=1L;
Hashtable receivedContentTypes=新Hashtable();
/**
*维护包含已接收文件及其内容类型的列表。
*/
Hashtable receivedFiles=新Hashtable();
/**
*重写executeAction以将收到的文件保存在自定义位置
*并从会话中删除此项目。
*/
@凌驾
公共字符串执行操作(HttpServletRequest请求,列表会话文件)引发UploadActionException{
字符串响应=”;
int cont=0;
对于(文件项:会话文件){
if(false==item.isFormField()){
cont++;
试一试{
///基于客户端中的远程文件名创建新文件

//String saveName=item.getName().replaceAll(“[\\\\/>这不是我的解决方案。已经有人回答了

您应该使用文件#createTempFile(),该文件采用一个目录

File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
item.write(file);
或者,如果以后确实要将临时文件移动到另一个位置,请使用file#rename to()


绝对一旦你收到文件rest是simple core java没有什么特别的。我尝试了很多方法,但都无法保存文件。我的客户端没有调用SampleUploadServlet,你知道如何让客户端调用SampleUploadServlet吗?在客户端,它工作正常&实际上它将图像放入客户端会话中。它说系统找不到路径吗阅读注释///在/tmp中创建一个临时文件(仅在unix中可用),并将其存储为.bin bot,即映像的实际ext
File destination = new File("/path/to/your/uploads", file.getName());
file.renameTo(destination);