2012-08-06 16 views
10

Tôi sử dụng Tải lên tệp đơn giản của Primefaces khi phát triển với Netbeans. Ví dụ kiểm tra của tôi tương tự như sổ tay Primefaces.
Câu hỏi của tôi: tệp được tải lên máy tính cục bộ của tôi ở đâu? Làm thế nào tôi có thể thay đổi đường dẫn cho nó? Cám ơn!Tệp p: fileUpload được tải lên được lưu ở đâu và làm cách nào để thay đổi?

File JSF:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
     <title>Page test</title> 
    </h:head> 
    <h:body> 
     Hello! My first JSF generated page! 
     <h:form enctype="multipart/form-data"> 
      <p:fileUpload value="#{fileBean.file}" mode="simple" /> 
      <p:commandButton value="Submit" ajax="false"/> 
     </h:form> 

    </h:body> 
</html> 

và bean được quản lý:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import org.primefaces.event.FileUploadEvent; 
import org.primefaces.model.UploadedFile; 


    @ManagedBean 

@RequestScoped 
public class FileBean { 

    private UploadedFile file; 

    public FileBean() { 
    } 

    public UploadedFile getFile() { 
     return file; 
    } 

    public void setFile(UploadedFile file) { 
     this.file = file; 

    } 
} 

Trả lời

18

Nó theo mặc định lưu trong hoặc bộ nhớ container servlet hoặc thư mục temp, tùy thuộc vào kích thước tập tin và cấu hình Tệp tải lên Apache Commons (xem phần "Cấu hình bộ lọc" của chương <p:fileUpload> trong PrimeFaces User's Guide).

Bạn không nên lo lắng về điều này chút nào. Thùng chứa servlet và PrimeFaces biết chính xác những gì chúng làm. Bạn nên thực hiện phương pháp hành động của nút lệnh thực sự là lưu nội dung tệp đã tải lên vào vị trí nơi bạn cần thiết. Bạn có thể lấy nội dung tệp được tải lên dưới dạng InputStream bởi UploadedFile#getInputStream() hoặc dưới dạng byte[] bởi byte[] là bộ nhớ có khả năng đắt tiền trong trường hợp tệp lớn, bạn biết, mỗi byte ăn một byte bộ nhớ của JVM, do đó, đừng làm điều đó trong trường hợp các tệp lớn).

Ví dụ:

<p:commandButton value="Submit" action="#{fileBean.save}" ajax="false"/> 

với

private UploadedFile uploadedFile; 

public void save() throws IOException { 
    String filename = FilenameUtils.getName(uploadedFile.getFileName()); 
    InputStream input = uploadedFile.getInputStream(); 
    OutputStream output = new FileOutputStream(new File("/path/to/uploads", filename)); 

    try { 
     IOUtils.copy(input, output); 
    } finally { 
     IOUtils.closeQuietly(input); 
     IOUtils.closeQuietly(output); 
    } 
} 

(FilenameUtilsIOUtils là từ Commons IO mà bạn nên nào đã đã cài đặt để có được <p:fileUpload> làm việc)

Để tạo tên tập tin độc đáo, bạn có thể tìm thấy cơ sở File#createTempFile() hữu ích.

String filename = FilenameUtils.getName(uploadedFile.getFileName()); 
String basename = FilenameUtils.getBaseName(filename) + "_"; 
String extension = "." + FilenameUtils.getExtension(filename); 
File file = File.createTempFile(basename, extension, "/path/to/uploads"); 
FileOutputStream output = new FileOutputStream(file); 
// ... 
+0

Cảm ơn bạn, tôi đánh giá cao sự giúp đỡ của bạn! Tôi nhận được logic của nó và nó chỉ hoạt động. – seinecle

+1

Bạn được chào đón. – BalusC

-1

tôi đã sử dụng chế độ đơn giản, GlassFish 4.0 và PrimeFaces 4,0

Sao Bean

private UploadedFile file; 
private String destination="C:\\temp\\"; 

public void upload() { 

    System.out.println("uploading"); 
    if(file != null) { 
     System.out.println("the file is" +file); 
     FacesMessage msg = new FacesMessage("Succesful" + file.getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 

     try { 
      copyFile(file.getFileName(), file.getInputstream()); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    System.out.println("uploaf finished"); 
} 

public void copyFile(String fileName, InputStream in) { 
    try { 
     // write the inputStream to a FileOutputStream 
     OutputStream out = new FileOutputStream(new File(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(); 

     System.out.println("New file created!"); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

trang JSF

<p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/>