2013-05-10 7 views
0

파일을 업로드하려고하지만 HTML 양식을 사용하고 있지 않습니다. QueryParam 및 PathParam은 사용할 수 없습니다. 그래서 아무도 스트림을 전달하는 방법을 말할 수 있습니다.웹 서비스에 매개 변수로 InputStream을 전달합니다.

try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     InputStream stream=new FileInputStream(new File("C:/localstore/ankita/Desert.jpg")); 
     String url="http://localhost:8080/Cloud/webresources/fileupload"; 
     HttpPost httppost = new HttpPost(url); 
     HttpResponse response = httpclient.execute(httppost); 
    } 
    catch(Exception e){} 

내 웹 서비스 클래스가 다소 다음과 같습니다 :

내 HttpClient를 같이 보인다

@Path("/fileupload") 
public class UploadFileService { 

@POST 
@Consumes(MediaType.APPLICATION_OCTET_STREAM) 

public Response uploadFile(InputStream in) throws IOException 
{  
    String uploadedFileLocation = "c://filestore/Desert.jpg" ; 

    // save it 
    saveToFile(in, uploadedFileLocation); 

    String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation; 

    return Response.status(200).entity(output).build(); 

} 

// save uploaded file to new location 
private void saveToFile(InputStream uploadedInputStream,String uploadedFileLocation) 
{ 
    try { 
     OutputStream out = null; 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     out = new FileOutputStream(new File(uploadedFileLocation)); 
     while ((read = uploadedInputStream.read(bytes)) != -1) 
     { 
      out.write(bytes, 0, read); 
     } 
     out.flush(); 
     out.close(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

} 

}

사람이 도와 드릴까요 ?

String url="http://localhost:8080/Cloud/webresources/fileupload"; 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(new File("C:/localstore/ankita/Desert.jpg")), -1); 
     reqEntity.setContentType("binary/octet-stream"); 
     reqEntity.setChunked(true); // Send in multiple parts if needed 
     httppost.setEntity(reqEntity); 
     HttpResponse response = httpclient.execute(httppost); 

웹 서비스는 어떻게 보입니까?

답변

1

그렇게 할 수 없습니다. 스트림은 직렬화 할 수 없으므로 HTTP 요청에서 스트림을 전달할 수 없습니다.

이다을 수행하는 방법은, 스트림을 래핑하는 HttpEntity를 작성 (예를 들어 InputStreamEntity) 다음 setEntity를 사용 HttpPOST 오브젝트에 첨부. 그런 다음 POST가 전송되고 클라이언트는 스트림에서 읽고 바이트를 요청의 "POST 데이터"로 보냅니다.

+0

답장을 보내 주셔서 감사합니다. 나는 이렇게하려고 노력할 것이다. 작동하는지 알려줄 것입니다. –

+0

다음과 같은 것이 있지만 웹 서비스가 이것을 어떻게 호출할까요? 끝에 코드를 추가했습니다. –

관련 문제