Java 将InputStream作为参数传递给web服务

Java 将InputStream作为参数传递给web服务,java,file-upload,jersey,Java,File Upload,Jersey,我试图上传文件,但我不是通过html表单。无法使用QueryParam和PathParam。所以,谁都知道如何通过这条小溪 我的HttPClient看起来像: try { HttpClient httpclient = new DefaultHttpClient(); InputStream stream=new FileInputStream(new File("C:/localstore/ankita/Desert.jpg")); St

我试图上传文件,但我不是通过html表单。无法使用QueryParam和PathParam。所以,谁都知道如何通过这条小溪

我的HttPClient看起来像:

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){}
我的web服务类看起来有点像:

@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);   

web服务将是什么样子?

您不能这样做。您不能在HTTP请求中传递流,因为流是不可序列化的


方法是创建一个
HttpEntity
来包装流(例如
InputStreamEntity
),然后使用
setEntity
将其附加到
HttpPOST
对象。然后发送POST,客户端将从您的流中读取并发送字节作为请求的“POST数据”。

感谢您的回复。我会尽量这样做。我会告诉你们它是否有效。我得到了下面的东西,但web服务将如何称呼它?在上面的末尾添加了代码。