我当前的设置服务器端
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.3</version>
</dependency>
我正在尝试将一个文件从AngularJS上传到我的服务器,但是它正在抛出'没有多内容请求的内容'
<form name="upload" class="form">
<input type="file" name="myImage" id="myImage" accept="image/*" onchange="angular.element(this).scope().attachedImage(this)"/>
</form>
我的控制器
$scope.attachedImage = function (element) {
$scope.$apply(function ($scope) {
$scope.myImage = element.files[0];
});
console.log('Image Attached');
};
function uploadFile() {
var fd = new FormData();
fd.append("myImage", $scope.myImage);
var service = UploadService.addImage(fd);
service.then(
function (response) {
console.log("Success : " + response);
}), function (error) {
console.log("Error : " + error);
};
};
我的服务在Service.js中定义
//API URL Defined
.constant('API_URL', 'http://localhost:4567/')
function UploadService($http, CORE_API_URL) {
return {
addImage: function (fd) {
return $http.post(API_URL + 'uploadToServer', fd
, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
);
}
}
}
最后我的服务器代码(SparkJava)
post("/uploadToServer", (req, res) -> {
try {
req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("D:/tmpNew"));
Part imagePart = req.raw().getPart("myImage");//Here I get Exception
try (InputStream inputStream = imagePart.getInputStream()) {
OutputStream outputStream = new FileOutputStream("D:/tmp/" + imagePart.getSubmittedFileName());
IOUtils.copy(inputStream, outputStream);
outputStream.close();
} catch (Exception ex) {
System.out.println(ex.getStackTrace());
}
} catch (Exception ex) {
System.out.println(ex.getStackTrace());
}
return "Success";
}, new JsonTransformer());
我得到的例外是:
WARN org.eclipse.jetty.server.Request -
java.io.IOException: Missing content for multipart request
at org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)
at org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)
at org.eclipse.jetty.server.Request.getParts(Request.java:2311)
at org.eclipse.jetty.server.Request.extractMultipartParameters(Request.java:495)
at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:417)
at org.eclipse.jetty.server.Request.extractParameters(Request.java:363)
at org.eclipse.jetty.server.Request.getParameter(Request.java:989)
at javax.servlet.ServletRequestWrapper.getParameter(ServletRequestWrapper.java:194)
at spark.Request.queryParams(Request.java:262)
at spark.webserver.RequestWrapper.queryParams(RequestWrapper.java:133)
at my.API.MyServices.lambda$main$66(MyServices.java:20)
at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
at org.eclipse.jetty.server.Server.handle(Server.java:517)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
at java.lang.Thread.run(Thread.java:748)
我几乎到处搜索它的解决方案都无济于事,需要帮助!