I've not use jersey-multipart before and am instead using Apache Commons
FileUpload. Are there advantages to using jersey-multipart? I'll have to
check into it.
Regardless, here's the way I use Apache Commons in case it helps or you'd
like an alternative solution. I just recently implemented it and haven't
cleaned it up yet. The FileInfo object contains information such as the
filename.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("image")
public Response uploadImage() {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold((int)maxFileSize);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(maxFileSize);
upload.setSizeMax(maxSize);
try {
@SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(request);
MediaDTO media = null;
for (FileItem item : items) {
if (!item.isFormField()) {
media = fileService.addImage(item);
break; // assume we only get one file at a time
}
}
if (media == null) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
URI uri =
uriInfo.getAbsolutePathBuilder().path(media.getMediaName().toString()).build();
media.setUrl(uri.toString());
return Response.created(uri).entity(media).build();
} catch (FileUploadException e) {
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (Exception e) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
Tauren
On Wed, Mar 23, 2011 at 8:34 AM, Pavel Bucek <pavel.bucek_at_oracle.com> wrote:
> hello,
>
> please see multipart-webapp sample:
>
> @Path("part-file-name")
>
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> @POST
> public String post(
> @FormDataParam("part") String s,
> @FormDataParam("part") FormDataContentDisposition d) {
> return s + ":" + d.getFileName();
> }
>
> you will need to introduce another dependency (if you are not using it
> already):
>
> <dependency>
> <groupId>com.sun.jersey.contribs</groupId>
> <artifactId>jersey-multipart</artifactId>
> <version>${project.version}</version>
> <!--<scope>provided</scope>-->
> </dependency>
>
> Regards,
> Pavel
>
>
> On 03/23/2011 04:04 PM, jmandawg wrote:
>
>> Hi all, i'm trying to upload a file to a jersey rest service. Here is my
>> code:
>>
>> @POST
>> @Path("/upload")
>> @Consumes(MediaType.MULTIPART_FORM_DATA)
>> public String doUpload(FormDataMultiPart data) {
>>
>> try{
>> FileOutputStream fos = new FileOutputStream(new
>> File("C:\\tmp\\test.tmp"));
>> byte[] fileData =
>> data.getField("Filedata").getValueAs(byte[].class);
>> fos.write(fileData);
>> fos.close();
>> }
>> catch(Exception ex)
>> {
>> ex.printStackTrace();
>> }
>> }
>>
>> My header looks like this:
>>
>> ------------Ef1ei4KM7ei4KM7GI3gL6KM7Ef1ei4
>>
>> Content-Disposition: form-data; name="Filedata";
>> filename="2009_Calendar.xls"
>>
>> Content-Type: application/octet-stream
>> binarydata.....
>>
>> My question is,
>> How do i retrieve the value of filename??
>>
>>
>> --
>> View this message in context:
>> http://jersey.576304.n2.nabble.com/How-to-pull-filename-out-of-FormDataMultiPart-tp6200588p6200588.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>
>>
>