Azure逻辑应用程序http发布multipart / form-data文件上传

时间:2017-09-07 20:31:17

标签: java azure http azure-logic-apps

我正在尝试使用Azure中的HTTP逻辑应用程序调用api

我可以通过邮递员使电话成功。看我的帖子man配置

Post Man Configuration

我可以看到来自postman的http代码,我正在使用它来使逻辑应用程序的格式类似于post man。

POST /dcma/rest/initiateOcrClassifyExtract HTTP/1.1

Host: godemo.ephesoft.com

Authorization: Basic NDU=??????

Cache-Control: no-cache

Postman-Token: 3baf23e7-6b46-a5f4-094b-3df1879bbe21

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="000001.pdf"; filename="000001.pdf"
Content-Type: application/pdf


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="batchClassIdentifier"

BC590

------WebKitFormBoundary7MA4YWxkTrZu0gW--

下面是实际的逻辑应用程序http配置。

Logic App config

服务器上的日志显示以下错误

    2017-09-07 20:12:51,784 [ajp-apr-8009-exec-3] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/dcma].[DispatcherServlet]- Servlet.service() for servlet [DispatcherServlet] in context with path [/dcma] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: Header section has more than 10240 bytes (maybe it is not properly terminated)] with root cause
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Header section has more than 10240 bytes (maybe it is not properly terminated)
    at org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:541)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:999)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:158)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:142)
    at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1070)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:912)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.AuthenticationFilter.doFilter(AuthenticationFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.HTTPHeaderFilter.doFilter(HTTPHeaderFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)

3 个答案:

答案 0 :(得分:1)

从您提供的日志消息中,我发现您在后端使用了apache.commons.fileupload库。 所以,我创建了一个简单的servlet web项目,其中包含如下的核心代码和apache.commons.fileupload库来处理上传的文件。

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet("/HelloWorldByPostman")
public class HelloWorldByPostman extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorldByPostman() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

//      String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");

        boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
        System.out.println(isMultipart);

        if(isMultipart){  
            FileItemFactory factory = new DiskFileItemFactory();  
            ServletFileUpload upload = new ServletFileUpload(factory);  
            List<FileItem> items = null;  
            try {
                items=upload.parseRequest(request);
                System.out.println(items.toString());  
            } catch (FileUploadException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
            Iterator<FileItem> iterator = items.iterator();  
            while(iterator.hasNext()){  
                FileItem item = iterator.next();  
                if(item.isFormField()){  
                    System.out.println("is txt........"+item.getFieldName());  
                }else{  
                    System.out.println("is file..........."+item.getFieldName());  
                }  
            }  
        }  

        response.getWriter().append("Served at: ").append("jaygong");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

然后,我将项目部署到Azure Web App,以便我可以通过http请求上传文件。

<强> 邮差:

enter image description here

enter image description here

<强> 结果: enter image description here

逻辑应用:

enter image description here

<强> 结果:

enter image description here

当操作为post且内容类型类型为multipart/form-data时,浏览器将采用表单控制单元细分,并为每个部分加上内容处理(表单数据或文件) ,Content-Type(默认为text / plain),名称(名称控件)和其他信息,并添加边界。

Content-Type部分已包含Body属性,因此您可以删除Content-Type部分中的Header设置,然后重试您的帖子请求。

希望它对你有所帮助。

答案 1 :(得分:1)

对于multipart / form-data,现在官方文档中有一个示例:

https://docs.microsoft.com/bs-latn-ba/azure/connectors/connectors-native-http#content-with-multipartform-data-type

"body": {
   "$content-type": "multipart/form-data",
   "$multipart": [
      {
         "body": "<output-from-trigger-or-previous-action>",
         "headers": {
            "Content-Disposition": "form-data; name=file; filename=<file-name>"
         }
      }
   ]
}

如果还有其他表单字段要发送,只需添加具有正确表单字段名称的相似对象即可。

答案 2 :(得分:-1)

您需要像这样传递数据 体内

{
  "$Content-Type": "application/x-www-form-urlencoded",
  "$formdata": [
    {
      "key": "grant_type",
      "value": "client_credentials"
    },
    {
      "key": "client_id",
      "value": "your clientid"
    },
    {
      "key": "client_secret",
      "value": "your client secret"
    }
  ]
}