2010-12-03 56 views
5

我有以下處理文件在服務器上上傳的代碼。但如何將文件保存到特定位置的服務器上將上傳的文件保存在特定位置

import gwtupload.server.UploadAction; 
import gwtupload.server.exceptions.UploadActionException; 

import org.apache.commons.fileupload.FileItem; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Hashtable; 
import java.util.List; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 




/** 
* This is an example of how to use UploadAction class. 
* 
* This servlet saves all received files in a temporary folder, 
* and deletes them when the user sends a remove request. 
* 
* @author Manolo Carrasco Moñino 
* 
*/ 
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 
      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) 
     { 
     } 
     } 
    } 

    /// 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(); 
    } 
    } 

} 

回答

9

您應該使用File#createTempFile() which takes a directory來代替。

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); 
1

在您的java應用程序中,您可以創建具有特定路徑的文件。

贊; 「新的文件(」 C:/文件/文件名)」。等

Java Documents瞭解新IO包的文件類的CreateFile函數

+0

這將拋出IO異常,如果 「C:/文件」目錄不存在確定新的文件(「c:/ files」)。mkdirs();如果不存在 – 2016-05-19 09:33:02

相關問題