2010-02-14 7 views
7

my ajax 애플리케이션은 사용자 브라우저에서 Java 애플리케이션 컨테이너로 파일을 업로드합니다. 제가하고 싶은 것은 이것입니다 : 일단 업로드가 완료되면 파일을 호스트 이름 (예 : localhost), 포트 (즉 8080) 및 원하는 위치로 식별되는 WebDAV 서버로 "전송"하고 싶습니다. 파일을 저장합니다 (예 : dir1/dir2).자바 : 서블릿에서 WebDAV 서버로 파일을 업로드하는 방법은 무엇입니까?

기본적으로 WebDAV 클라이언트 프레임 워크로 WebDAV에 파일을 업로드 할 수 있습니다. 내 응용 프로그램에서 이미 "webdavclient4j"를 사용하고 있지만 파일을 업로드하는 방법을 찾지 못하는 것 같습니다.

아이디어가 있으십니까? 제공 할 수있는 도움에 미리 감사드립니다.

F는

답변

12

당신의 코드 몇 줄을 사용하여 그것을 할 수 있습니다 내 최근 출시하고 사용하기 아주 쉽게 자바, 정어리에 대한 현대 webdav 클라이언트. 다음 (첫번째 파일을 읽어 몬즈 IO를 사용하는) 몇 예이다

Sardine sardine = SardineFactory.begin("username", "password"); 
byte[] data = FileUtils.readFileToByteArray(new File("/file/on/disk")); 
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", data); 

또는 사용 스트림 :

Sardine sardine = SardineFactory.begin("username", "password"); 
InputStream fis = new FileInputStream(new File("/some/file/on/disk.txt")); 
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", fis); 

https://github.com/lookfirst/sardine

환호

+0

안녕하세요, 답변 해 주셔서 감사합니다. 나는 Sardine을 시험해보고 내 응용 프로그램에서 사용하기로 결정했습니다. 일을 훨씬 쉽게 해줍니다. F – francescoNemesi

+0

나도 Sardine을 구현했습니다. 놀랍게도 간단하게 웹 서비스를 만들 수 있습니다. 이제는 모든 테스트를 거쳐야합니다.) –

+0

@ 존 : 직접 디렉토리를 업로드 할 수있는 방법이 있습니까? 그리고 웹 서버에 디렉토리가 있다면 파일을 덮어 씁니까? – rkg

6

당신은 Jackrabbit WebDAV Library를 사용할 수 있습니다.

(here에서 가져온) WebDAV 서버에 콘텐츠를 업로드하는 WebDAV 클라이언트의 예 :

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.URL; 
import org.apache.commons.httpclient.Credentials; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpException; 
import org.apache.commons.httpclient.UsernamePasswordCredentials; 
import org.apache.commons.httpclient.auth.AuthScope; 
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 
import org.apache.commons.httpclient.methods.RequestEntity; 
import org.apache.jackrabbit.webdav.client.methods.PutMethod; 

... 

// WebDAV URL: 
final String baseUrl = ...; 
// Source file to upload: 
File f = ...; 
try{ 
    HttpClient client = new HttpClient(); 
    Credentials creds = new UsernamePasswordCredentials("username", "password"); 
    client.getState().setCredentials(AuthScope.ANY, creds); 

    PutMethod method = new PutMethod(baseUrl + "/" + f.getName()); 
    RequestEntity requestEntity = new InputStreamRequestEntity(
     new FileInputStream(f)); 
    method.setRequestEntity(requestEntity); 
    client.executeMethod(method); 
    System.out.println(method.getStatusCode() + " " + method.getStatusText()); 
} 
catch(HttpException ex){ 
    // Handle Exception 
} 
catch(IOException ex){ 
    // Handle Exception 
} 
+0

나는 당신이 게시 한 코드를 얻으려고 노력했다. Tomcat에 대한 슬픔의 종식을 초래했습니다. 우리 WebDAV 서버는 Jackrabbit을 기반으로하므로 실제로 작동하도록하고 싶습니다. –

+0

감사합니다. 오라클 webdav 서버 (Sardine과 달리)를 통해 도움이되었습니다. –

관련 문제