我有一个用户可以上传图片的页面。我使用angular js的post请求将数据发布到url。我怀疑的是如何在servlet中检索这些数据。我想将图像保存在数据库中。如果这是执行此活动的正确方法,请告诉我
//Controller
app.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' + (file));
var uploadUrl = "/Angular/login";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
//Service
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);
//directive
app.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
我应该如何在java servlet中检索上面发布的数据..?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
答案 0 :(得分:0)
您可以安全地使用Apache Commons FileUpload,这也适用于较旧的servlet版本。
检查http://commons.apache.org/proper/commons-fileupload/using.html
<!DOCTYPE html>
<html ng-app="FileuploadApp">
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
angular
.module("FileuploadApp", [])
.controller("FileCtrl", function($scope, $http) {
$scope.upload = function() {
var fd = new FormData();
fd.append("file", $scope.file);
$http({
withCredentials: true,
method: 'POST',
url: './fileupload',
data: fd,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
});
}
})
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
</script>
</head>
<body ng-controller="FileCtrl">
<input type="file" file-model="file"/>
<input type="button" value="Upload!" ng-click="upload();">
</body>
</html>
@WebServlet(urlPatterns="/fileupload")
public class FileuploadTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
if (ServletFileUpload.isMultipartContent(req)) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
ServletFileUpload fileUpload = new ServletFileUpload(factory);
List<FileItem> files = fileUpload.parseRequest(req);
if (files != null && !files.isEmpty()) {
for (FileItem item : files) {
System.out.println("* " + item.getName() + " " + item.getSize() + " bytes.");
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
}
* cthulhu.bin 42 bytes.
答案 1 :(得分:-1)
您可以使用InputStream stream = request.getInputStream();
从servlet请求中读取原始数据