Restlet& MULTIPART_FORM_DATA或其他方式通过Restlet将文件放在Google App Engine上 [英] Restlet & MULTIPART_FORM_DATA or another way to put files on Google App Engine via Restlet

查看:476
本文介绍了Restlet& MULTIPART_FORM_DATA或其他方式通过Restlet将文件放在Google App Engine上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过restlet接收文件,但只获取完整的MULTIPART_FORM_DATA。
我如何提取我的特定文件?

我发现了一些代码块,但它们的类型不可用...
RESTlet:如何处理多部分/表单数据请求?

  DiskFileItemFactory factory = new DiskFileItemFactory(); 
factory.setSizeThreshold(1000240);
$ b $ //创建一个新的文件上传处理程序
RestletFileUpload upload = new RestletFileUpload(factory);

我的当前代码

  @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);

尝试{
Upload2(restletRequest,entity);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();

$ b $ 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();

使用这个方法存储后,我有类似的东西,我只想存储名称的内容picture:

   z------ WebKitFormBoundarysOvzWKPqyqW7DiTu 
Content-Disposition:form-data; NAME = 图片;内容类型:image / jpeg

* ExifII *

据我读的多部分表单数据的解析不支持呢?但必须有一个解决方案,通过restlet发送文件



我试着通过邮递员的其余的调用铬。只是在多部分支持文件。是一个可能的解决方案发送图像作为原始文本?

解决方案

您将需要添加例外处理和处理 InputStream 并且可能清理临时文件(参见 DiskFileItemFactory docs),但使用org.restlet.ext.fileupload库时的基础如下。

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



$ / code $ / pre

DiskFileItemFactory gwtupload.server.MemoryFileItemFactory


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?

解决方案

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.

这篇关于Restlet&amp; MULTIPART_FORM_DATA或其他方式通过Restlet将文件放在Google App Engine上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆