[英]Nodejs: Unirest: How to upload file with multipart/form-data
I have a nodejs application, which is trying to upload a file to a springboot application by calling an API using unirest.我有一个 nodejs 应用程序,它试图通过使用 unirest 调用 API 将文件上传到 springboot 应用程序。
Below is my code:下面是我的代码:
var express = require('express');
var unirest = require("unirest");
var fileUpload = require('express-fileupload');
var fs = require('fs');
var app = express();
app.use(fileUpload());
app.all('/api/upload', function(req, res, next) {
restClient(req,res).then(function(result){
let data = result.body;
res.send(data);
}).catch(function (err) {
let data = err.error;
res.status(err.statusCode).send(data);
});
});
function restClient(req, res) {
return new Promise(function(resolve, reject) {
var reqClient = unirest(req.method, req.url);
let file = req.files.file;
let filepath = file.name;
file.mv(filepath, function(err) {
if (err) reject(err);
reqClient.headers({
"cache-control": "no-cache",
"Connection": "keep-alive",
"Content-Length": req.headers['Content-Length'],
"Accept-Encoding": "gzip, deflate",
"Host": "localhost:8080",
"Cache-Control": "no-cache",
"Accept": "*/*",
"Authorization": "Bearer " + req.session.access_token,
"Content-Type": "application/x-www-form-urlencoded",
"content-type": req.headers['content-type']
});
reqClient.multipart([
{
"body": fs.createReadStream(filepath)
},
{
"body": "fileType"
}
]);
});
reqClient.end(function(resClient) {
if (resClient.error) {
reject(resClient);
} else {
resolve(resClient);
}
});
});
}
I'm Getting the following error from the Spring-boot application.我从 Spring-boot 应用程序中收到以下错误。
org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Header section has more than 10240 bytes (maybe it is not properly terminated)
at org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:490) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:889) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:854) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.connector.Request.parseParts(Request.java:2734) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.connector.Request.parseParameters(Request.java:3073) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.connector.Request.getParameter(Request.java:1095) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at com.ge.bfe.controller.CorsFilter.doFilter(CorsFilter.java:34) ~[classes/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.28.jar:8.0.28]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.28.jar:8.0.28]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.28.jar:8.0.28]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]
And I have set multipart size also in application.properties file like below:我还在 application.properties 文件中设置了多部分大小,如下所示:
multipart.max-file-size=50MB
multipart.max-request-size=50MB
when I'm hitting from postman it's working.当我从邮递员那里打来时,它正在工作。 And the upload file size is 11KB.
上传文件大小为11KB。
Therefore, what could be the possible reason?因此,可能的原因是什么?
Perhaps change application.properties as below也许更改 application.properties 如下
spring.servlet.multipart.max-file-size=50MB spring.servlet.multipart.max-request-size=50MB
For spring boot 1.x try this spring.http.multipart.max-file-size=50MB spring.http.multipart.max-request-size=50MB spring.http.multipart.enabled=true
Also, Check this out : https://config9.com/apps/tomcat/how-to-set-the-max-size-of-upload-file/对于 spring boot 1.x 试试这个
spring.http.multipart.max-file-size=50MB spring.http.multipart.max-request-size=50MB spring.http.multipart.enabled=true
另外,看看这个: https ://config9.com/apps/tomcat/how-to-set-the-max-size-of-upload-file/
I guess the way you are uploading file may not be correct.我猜您上传文件的方式可能不正确。 you can try below code:
你可以试试下面的代码:
unirest
.post('http://mockbin.com/request')
.headers({'Content-Type': 'multipart/form-data'})
.field('parameter', 'value') // Form field
.attach('file', 'path to file or pass stream here') // Attachment
.then(function (response) {
console.log(response.body)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.