Hi

On Thu, Feb 3, 2011 at 2:29 PM, acec acec <[email protected]> wrote:

> Hi, all
> I am using the following code to upload a file. It works fine.
>
> But I have several questions:
> 1) If I change body.getAllAttachments() ==> body.getAttachment(contentId),
> What does mean "contentId"? In my junit code, How can I set that contentId?
>
> Content-Id the id of the individual multi-part; sure, if you know the
contentId then you can use

body.getAttachment(contentId)


> 2)Can I get original file name on server side? I tried
> dataHandler.getName(), but it return null.
>
>
If it's a multipart/form-data then you can get a ContentDisposition header
from the Attachment and get the name; otherwise you can just introduce a
custom header and use it during the creation of the individual part and then
retrieve it from Attachment.


> 3)In my junit code, when I add Part using (reqEntity.addPart("file1",
> fileBody)), how can I retrieve the name "file1" from my server side?
>
> You're using the the Apache Http Client, so I'm not sure what exactly it
produces. If it's a multipart/form-data request then on the cxf side do
attachment.getContentDisposition() and get the file name


> 4)I never use MTOM, when I am using the following code to upload one PDF
> file, does is use base64?
>
>
Not sure what Apache HttpClient does there

Cheers, Sergey


> Thanks a lot.
>
> ===============Server side coe===============================
> @POST
> @Path("/upload/book")
> public void postBook(MultipartBody body) {
>  try {
>  System.out.println("start post");
>  List<Attachment> attachments = body.getAllAttachments();
>  DataHandler dataHandler = attachments.get(0).getDataHandler();
>  InputStream inputStream = dataHandler.getInputStream();
>  OutputStream outputStream = new FileOutputStream(new
> File("/tmp/new.pdf"));
>  IOUtils.copy(inputStream, outputStream);
>  inputStream.close();
>  outputStream.close();
>  System.out.println("end post");
> } catch (Exception e) {
>  System.out.println("Exception");
> }
> }
>
> ====================My Junit==========================
> public class UploadClassTest {
>  @Test
>  public void uploadClass() throws ClientProtocolException, IOException {
>   HttpClient httpClient = new DefaultHttpClient();
>   HttpPost httpPost = new HttpPost("
> http://localhost:8080/spring_cxf_rest/rest/upload/book";);
>
>   FileBody fileBody = new FileBody(new File("/tmp/Citi.pdf"));
>
>   MultipartEntity reqEntity = new MultipartEntity();
>   reqEntity.addPart("file1", fileBody);
>
>   httpPost.setEntity(reqEntity);
>
>   httpClient.execute(httpPost);
>  }
> }
>
>
>

Reply via email to