问题
I tried to receive files via restlet but only gets the complete MULTIPART_FORM_DATA. How can I extract my specific file?
I found some code-blocks but the types of them are not available... RESTlet: How to process multipart/form-data requests?
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1000240);
// 2/ Create a new file upload handler
RestletFileUpload upload = new RestletFileUpload(factory);
My current code:
@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 storing with this method I have something like that and I only want to store the content with the name "picture":
��z------WebKitFormBoundarysOvzWKPqyqW7DiTu
Content-Disposition: form-data; name="picture"; filename="_MG_4369.jpg"
Content-Type: image/jpeg
����*ExifII*
As far as I read parsing of multipart form data isn’t supported yet? But there must be a solution to send files via restlet
I tried the rest-calls via postman for chrome. The is only on multiparts the support for files. Is a possible solution to send the image as a raw-text?
回答1:
You will need to add Exception
handling and deal with the InputStream
and potentially clean up of temp files (see DiskFileItemFactory
docs) but the basics are as follows, when 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 a gae Solution try replacing the DiskFileItemFactory
with a gwtupload.server.MemoryFileItemFactory
.
回答2:
You should send your file uploads as an InputStream
. Then you can use this:
@Put
public void handleUpload(InputStream entity) {
}
The file is now its stream and will no longer have form data inside of it. To send a file as a stream, you can set up a client in java (using jersey, for example).
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 set up a client to post data
to your server. I was trying this using multipart form data and postman before. I went mad trying to get multipart data to work. But this is the solution I have been using instead. And it works perfectly.
来源:https://stackoverflow.com/questions/18161517/restlet-multipart-form-data-or-another-way-to-put-files-on-google-app-engine-v