Thursday, 15 July 2010

java - @MultipartForm How to get the original file name? -



java - @MultipartForm How to get the original file name? -

i using jboss's rest-easy multipart provider importing file. read here http://docs.jboss.org/resteasy/docs/1.0.0.ga/userguide/html/content_marshalling_providers.html#multipartform_annotation regarding @multipartform because can map pojo.

below pojo

public class softwarepackageform { @formparam("softwarepackage") private file file; private string contentdisposition; public file getfile() { homecoming file; } public void setfile(file file) { this.file = file; } public string getcontentdisposition() { homecoming contentdisposition; } public void setcontentdisposition(string contentdisposition) { this.contentdisposition = contentdisposition; } }

then got file object , printed absolute path , returned file name of type file. extension , uploaded file name lost. client trying upload archive file(zip,tar,z)

i need info @ server side can apply un-archive programme properly.

the original file name sent server in content-disposition header.

how can information? or atleast how can jboss save file uploaded file name , extension? configurable application?

after looking around bit resteasy examples including one, seems there no way retrieve original filename , extension info when using pojo class @multipartform annotation.

the examples have seen far retrieve filename content-disposition header "file" part of submitted multiparts form info via http post, essentially, looks like:

content-disposition: form-data; name="file"; filename="your_file.zip" content-type: application/zip

you have update file upload rest service class extract header this:

@post @path("/upload") @consumes("multipart/form-data") public response uploadfile(multipartformdatainput input) { string filename = ""; map<string, list<inputpart>> formparts = input.getformdatamap(); list<inputpart> inpart = formparts.get("file"); // "file" should match name attribute of html file input (inputpart inputpart : inpart) { seek { // retrieve headers, read content-disposition header obtain original name of file multivaluedmap<string, string> headers = inputpart.getheaders(); string[] contentdispositionheader = headers.getfirst("content-disposition").split(";"); (string name : contentdispositionheader) { if ((name.trim().startswith("filename"))) { string[] tmp = name.split("="); filename = tmp[1].trim().replaceall("\"",""); } } // handle body of part inputstream inputstream istream = inputpart.getbody(inputstream.class,null); /* ..etc.. */ } grab (ioexception e) { e.printstacktrace(); } } string msgoutput = "successfully uploaded file " + filename; homecoming response.status(200).entity(msgoutput).build(); }

hope helps.

java web-services java-ee jboss resteasy

No comments:

Post a Comment