2013-02-02 28 views
37

Tôi đang cố gắng tạo thư mục cho mỗi tên người dùng mà người dùng đăng nhập bằng. Hiện tại tôi cóTạo các thư mục trung gian nếu một thư mục không tồn tại

private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads 
File theFile = new File(destination + username); // will create a sub folder for each user 

nhưng bit File theFile không tạo thư mục mới cho tên người dùng. Làm thế nào tôi sẽ làm điều này?

Tôi đã thử

private String destination; 

public void File() 
{ 
    destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads 
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 
    theFile.mkdirs(); 
} 

nhưng tôi cần phải sử dụng đến sau này trong chương trình, thế nào tôi sẽ làm điều đó?

Đây là toàn bộ mã của tôi:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package richard.fileupload; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import javax.annotation.PostConstruct; 
import javax.faces.application.FacesMessage; 
import javax.faces.context.FacesContext; 
import javax.faces.event.ActionEvent; 
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext; 
import java.io.File; 
import org.primefaces.event.FileUploadEvent; 

@ViewScoped 
@ManagedBean(name = "fileUploadController") 
public class FileUploadController { 

    /* 
    public void handleFileUpload(FileUploadEvent event) { 
    System.out.println("called"); 
    FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); 
    FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 
    } 
    */ 
    private String username; 
    private String destination; 

    @PostConstruct 
    public void init() { 
     System.out.println("called get username"); 
     username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser(); 
    } 

    public void File() { 
    destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads 
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 
    theFile.mkdirs(); 
} 

    public File getDirectory(String destination, String username) { 
     System.out.println("called get directory"); 
     // currently not working, is not calling the username or destination 
     //set the user directory from the destinarion and the logged user name 
     File directory = new File(destination, username); 
     //check if the location exists 
     if (!directory.exists()) { 
      //let's try to create it 
      try { 
       directory.mkdir(); 
      } catch (SecurityException secEx) { 
       //handle the exception 
       secEx.printStackTrace(System.out); 
       directory = null; 
      } 
     } 
     return directory; 
    } 

    public void handleFileUpload(FileUploadEvent event) { 
     System.out.println("called handle file"); 
     FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); //Displays to user on the webpage 
     FacesContext.getCurrentInstance().addMessage(null, msg); 
     try { 
      copyFile(event.getFile().getFileName(), event.getFile().getInputstream()); 
     } catch (IOException e) { 
      //handle the exception 
      e.printStackTrace(); 
     } 
    } 

    public void copyFile(String fileName, InputStream in) { 
     try { 


      // write the inputStream to a FileOutputStream 
      OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm 
      System.out.println("Called CopyFile"); //testing 
      System.out.println(destination + fileName); 

      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = in.read(bytes)) != -1) { 
       out.write(bytes, 0, read); 
      } 

      in.close(); 
      out.flush(); 
      out.close(); 
//make sure new file is created, (displays in glassfish server console not to end user) 
      System.out.println("New file created!");//testing 
     } catch (IOException e) { 
      e.printStackTrace(); 

      FacesMessage error = new FacesMessage("The files were not uploaded!"); 
      FacesContext.getCurrentInstance().addMessage(null, error); 
     } 
    } 
} 

EDIT CUỐI CÙNG (Hy vọng)

public void copyFile(String fileName, InputStream in) { 
     try { 

      destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads 
      File theFile = new File(destination + "/" + username); 
      theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK) 

      System.out.println("Completed File"); 
      // write the inputStream to a FileOutputStream 
      OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm 
      System.out.println("Called CopyFile"); //testing 
      System.out.println(destination + fileName); 

      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = in.read(bytes)) != -1) { 
       out.write(bytes, 0, read); 
      } 

      in.close(); 
      out.flush(); 
      out.close(); 
//make sure new file is created, (displays in glassfish server console not to end user) 
      System.out.println("New file created!");//testing 
     } catch (IOException e) { 
      e.printStackTrace(); 

      FacesMessage error = new FacesMessage("The files were not uploaded!"); 
      FacesContext.getCurrentInstance().addMessage(null, error); 
     } 
    } 
} 

Chỉ cần làm thế nào tôi có thể in ra các điểm đến mới và sử dụng sau này như hiện nay nó tạo thư mục mới nhưng không chọn nó để sử dụng

CHỈNH SỬA GIẢI PHÁP NÀY:

NewDestination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/" + username; 

Đã thêm mã ở trên và bây giờ tất cả hoạt động

+1

thể trùng lặp của [Tạo toàn bộ con đường tự động khi viết vào một tập tin mới] (http://stackoverflow.com/questions/2833853/create-whole-path-automatically -khi-writing-to-a-new-file) –

Trả lời

102

Bạn phải thực sự gọi một số phương pháp để tạo thư mục. Chỉ cần tạo đối tượng file sẽ không tạo tệp hoặc thư mục tương ứng trên hệ thống tệp.

Bạn có thể sử dụng File#mkdirs() phương pháp để tạo ra các thư mục: -

theFile.mkdirs(); 

Sự khác nhau giữa File#mkdir()File#mkdirs() là, sau này sẽ tạo ra bất kỳ thư mục trung gian nếu nó không tồn tại.

+0

Cảm ơn một bó :) sẽ kiểm tra nó ra: D – user1924104

+1

@ user1924104 .. Bạn đang chào đón :) –

+0

câu hỏi ngớ ngẩn, im vẫn cực kỳ mới ở java, nhưng làm thế nào tôi sử dụng mkdirs? ive xem xét một vài ví dụ và tất cả chúng đều được sử dụng theo các cách khác nhau – user1924104

14

sử dụng mã spinen này cho tạo các thư mục trung gian nếu ta không tồn tại trong khi tạo/chỉnh sửa file:

File outFile = new File("/dir1/dir2/dir3/test.file"); 
outFile.getParentFile().mkdirs(); 
outFile.createNewFile(); 
+0

Vui lòng chỉnh sửa với nhiều thông tin hơn. Các câu trả lời chỉ có mã và "dùng thử" này không được khuyến khích, bởi vì chúng không chứa nội dung có thể tìm kiếm được và không giải thích tại sao một người nào đó nên "thử cái này". Chúng tôi nỗ lực ở đây để trở thành một nguồn lực cho kiến ​​thức. – abarisone

+0

câu trả lời này là câu hỏi làm thế nào để tạo một tập tin từ một chuỗi một cách dễ dàng – Mishax

Các vấn đề liên quan