RestFul Webservices文件上传

时间:2016-06-08 14:19:33

标签: java web-services rest file-upload apache-commons-fileupload

我想从jsp上传文件,在我的servlet中处理它,然后从servlet传递与Restful webservices相同的文件。

HTML:

<html>
<body>
    <h1>Upload File with RESTFul WebService</h1>
    <form action="rest/fileupload" method="post" enctype="multipart/form-data">
       <p>
        Choose a file : <input type="file" name="file" />
       </p>
       <input type="submit" value="Upload" />
    </form>
</body>
</html>

的Servlet

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
FileBody fileContent= new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", fileContent);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

但是在这里,当我从我自己的servlet中的jsp获取文件时,由于Multipart Form Data,我只能将它作为FileItem获取。现在,我如何将FileItem放在FileBody中,以便将其作为MultipartEntity发送。请注意,我不想直接从jsp页面调用Webservices。

0 个答案:

没有答案