2012-09-20 4 views
1

내 JSF 응용 프로그램에 org.apache.commons.net.ftp.FTPClient을 사용하고 싶습니다. 클라이언트 측 (웹 브라우저)이 웹 응용 프로그램 서버에 대용량 파일 업로드 방법. RichFaces File Upload 또는 PrimeFaces File Upload을 사용하는 경우에도 클라이언트 브라우저는 HTTP Protocol을 사용할 수 있습니다. 클라이언트 브라우저에 FTP Protocol을 어떻게 지원합니까? 더 나은 방법을 제공해 줄 수 있습니까?JSF에서 FTP 파일 업로드를 사용하는 방법은 무엇입니까?

원인 : 응용 프로그램 사용자가 Repository Server(SVN)에 대한 액세스를 직접 할 수 없습니다. 먼저 파일을 Web AS에 애플리케이션에 업로드해야합니다. 그런 다음 응용 프로그램은 checkin/chekout ~ RepositoryServer입니다. 응용 프로그램 사용자는 최소한 500M에서 2G까지의 파일을 업로드 할 수 있습니다. 그것이 내가 생각하기에, 어떻게하면 브라우저 클라이언트에 FTP Protocol을 더 빨리 지원할 수 있을까요? 그렇지 않으면 내가 잘못 생각하고 있니?

+1

대용량 파일을 업로드하기 위해 RichFaces 파일 업로드 또는 PrimeFaces 파일 업로드를 사용하지 않는 이유는 무엇입니까? – Daniel

답변

1

파일을 FTP 서버로 전송하려면 분명히 FTP 클라이언트가 있어야합니다.

그러나 웹 브라우저는 FTP 클라이언트가 아닌 HTTP 클라이언트입니다. 이것은 웹 브라우저의 자연스러운 기능 설계 제한 사항입니다. JSF는 마술사처럼 보이지만 여기서는 정말로 당신을 위해 아무것도 할 수 없습니다. HTTP 요청/응답 만 차단합니다.

실제로, 당신은 잘못 생각하고 있습니다. 일반적인 HTTP 방식으로 파일을 업로드하는 데 전념하십시오. 만약 당신이 절대적으로 긍정 당신이 어떤 이유로 FTP가 필요하다면, 가장 좋은 내기가 가장 가능성이 자서전을위한 자바 애플릿이지만, 이것은 결국 어색한 것입니다.

0

먼저 HTTP를 primefaces를 통해 임시 디렉토리에 업로드하십시오. org.apache.commons.net.ftp.FTPClient 또는 sun.net.ftp.FtpClient를 통해 필요한 FTP 서버에 업로드하십시오.

다음은 예입니다.

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import sun.net.ftp.FtpClient; 

/** 
* 
* @author fali 
*/ 
public class FtpUtil { 

    public String server, username,password, remote, remotedir, local; 
    FtpClient ftp; 
    public static int BUFFER_SIZE = 10240; 
    public FtpUtil(){ 
     server = "localhost"; 
     username = "anonymous"; 
     password = " "; 
     remotedir = "/incoming"; 
     remote = "dvs.txt"; 
     local = "C:\\dvs.txt"; 

    } 
    protected void putFile() { 
    if (local.length() == 0) { 
     System.out.println("Please enter file name"); 
    } 
    byte[] buffer = new byte[BUFFER_SIZE]; 
    try { 
     File f = new File(local); 
     int size = (int) f.length(); 
     System.out.println("File " + local + ": " + size + " bytes"); 
     System.out.println(size); 
     FileInputStream in = new FileInputStream(local); 
     OutputStream out = ftp.put(remote); 

     int counter = 0; 
     while (true) { 
     int bytes = in.read(buffer); 
     if (bytes < 0) 
      break; 
     out.write(buffer, 0, bytes); 
     counter += bytes; 
     System.out.println(counter); 
     } 

     out.close(); 
     in.close(); 
    } catch (Exception ex) { 
     System.out.println("Error: " + ex.toString()); 
    } 
    } 

    public String Upload(){ 
     String result=""; 
     try{ 
     ftp = new FtpClient(server); 
     ftp.login(username, password); 
     System.out.println(ftp.welcomeMsg); 
     ftp.cd(remotedir); 
     putFile(); 
     disconnect(); 
     }catch(Exception ex){ 
      System.out.println(ex); 
      result = "Error : "+ex; 
     } 
     return ""; 
    } 
    protected void disconnect() { 
    if (ftp != null) { 
     try { 
     ftp.closeServer(); 
     } catch (IOException ex) { 
     } 
     ftp = null; 
    } 
    } 
} 

귀하의 managedbean/컨트롤러에서;

public String create() { 
     System.out.println("Request Button Clicked"); 
     try { 
      // generate reference number 
      //current.setReferenceno(genReferenceNo()); 
      // add to database 
      //getFacade().persist(current); 

      // upload to ftp 
      FtpUtil fu = new FtpUtil(); 
      fu.Upload(); 
      // show reference number 

      JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("QueueCreated")); 
      JsfUtil.addSuccessMessage("Your Reference No. is :" + current.referenceno); 
      current = null; 
//   try { 
//    System.out.println("Redirecting"); 
//    FacesContext.getCurrentInstance().getExternalContext().dispatch("/"); 
//   } catch (Exception ex) { 
//    System.out.println(ex); 
//   }    
      return ""; 
     } catch (Exception e) { 
      JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured")); 
      return null; 
     } 
    } 

귀하의 페이지에 다음과 같은 몇 가지 사항이 있습니다.

<br />      
       <ppctu:commandButton action="#{appointmentController.create}" type="Submit" value="Request" /> 
+0

OP가 웹 페이지 (HTML)에서 FTP로 바로 업로드하는 방법을 묻습니다. OP는 Java 클래스 (backing bean)에서 FTP로 업로드하는 방법을 묻지 않았습니다. OP는 이미 그 부분을 알고 있음을 암시합니다 (그리고 솔직히, 당신의'FtpUtil'은 더 잘할 수 있습니다 ...). – BalusC

+0

@BalusC : 나는 당신이 옳다는 것을 이해하고 그것은 또 다른 일이었고, 나는 FtpUtil이 완벽 할 수 있다는 것을 알고 있지만 단지 빠른 예일 뿐이라는 것을 알고 있습니다. –

관련 문제