Restlet & MULTIPART_FORM_DATA or another way to put files in the Google App Engine through Restlet

I tried to receive files through restayl, but received only MULTIPART_FORM_DATA. How can I extract my file?

I found several blocks of code, but their types are not available ... RESTlet: how to handle requests for multipart / form-data?

DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1000240); // 2/ Create a new file upload handler RestletFileUpload upload = new RestletFileUpload(factory); 

My current code is:

 @Put public void handleUpload(Representation entity) { if (entity !=null) { if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { Request restletRequest = getRequest(); Response restletResponse = getResponse(); HttpServletRequest servletRequest = ServletUtils.getRequest(restletRequest); HttpServletResponse servletResponse = ServletUtils.getResponse(restletResponse); try { Upload2(restletRequest, entity); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } public void Upload2(Request req, Representation entity) throws IOException { ... GcsOutputChannel outputChannel = gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance()); ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(outputChannel)); copy(entity.getStream(), oout); oout.close(); 

After saving with this method, I have something like this, and I only want to save the contents with the name "picture":

   z------WebKitFormBoundarysOvzWKPqyqW7DiTu Content-Disposition: form-data; name="picture"; filename="_MG_4369.jpg" Content-Type: image/jpeg     *ExifII* 

How far have I read parsing data in multipart form is not yet supported? But there must be a solution for sending files via restlet

I tried the rest of the calls through the chrome postman. Only support for multiple files is supported. Possible solution for sending an image as raw text?

+4
source share
2 answers

You will need to add Exception handling and handle it with an InputStream and potentially clear temporary files (see DiskFileItemFactory docs), but the basics are the following: using the org.restlet.ext.fileupload library.

 @Put public void handleUpload(Representation entity) { List<FileItem> items = new RestletFileUpload(new DiskFileItemFactory()) .parseRepresentation(representation); for (FileItem item : items) { if (!item.isFormField()) { MediaType type = MediaType.valueOf(item.getContentType()); InputStream inputStream = item.getInputStream(); } } } 

for gae Solution, try replacing DiskFileItemFactory with gwtupload.server.MemoryFileItemFactory .

0
source

You must send your files as an InputStream . Then you can use this:

 @Put public void handleUpload(InputStream entity) { } 

The file is now its stream and will no longer contain form data inside it. To send a file as a stream, you can configure the client in java (for example, using jersey).

 import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; File f = new File("C:/file/to/upload.zip"); InputStream data = new FileInputStream(f); Client client = Client.create(); client.setChunkedEncodingSize(1024); WebResource resource = client.resource("http://localhost:80/your/uri"); ClientResponse response = resource.accept("application/x-octet-stream").type("application/x-octet-stream").post(ClientResponse.class, data); 

Now that you have data , you can configure the client to send data to your server. I used to try using data with multiple data and a postman. I went crazy trying to get multi-page data for work. But this is the solution that I use instead. And it works great.

0
source

Source: https://habr.com/ru/post/1496305/


All Articles