我一直这样做,因为两天仍未找到任何解决方案, 我在eclipse工作区的图像文件夹中上传图片,当图像第一次上传时出现问题,然后需要刷新工作区中的图像文件夹然后清空缓存重新加载后才能正常工作
这是我的HTML
</div><img ng-src="{{image}}" alt="image caption" ngf-src="imagePath" style="height: 150px;width: 150px;margin: 10px;" ng-show="imagevisible"></div>
这是我的控制器
$scope.uploadFiles = function(file) {
debugger;
$scope.fileObj = file;
$scope.fileName = $scope.fileObj.name;
console.log($scope.fileObj.name);
}
$scope.submit=function(){
Upload.upload({
//method : 'POST',
url : '/WarehouseMgmt/material/add_mtr',
data : {
model : $scope.NewMtr,
file : $scope.fileObj
},
// headers : {'Content-Type': 'application/json; charset=utf-8'}
}).then(function(response){
debugger;
//$mdDialog.cancel();
$scope.showAddMtrlToast();
$window.location.href="#!/Material/"
$route.reload();
},function(response){
debugger;
console.log(response.status);
if(response.status==500){
$scope.showMtrlExistToast();
}
});
};
}
和我上传图片的java控制器方法
public String uploadMaterialImage(MultipartFile fileObj) {
Properties properties = new Properties();
String filename = "views.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return "";
}
try {
properties.load(input);
pathSave = properties.getProperty("saveImagePath");
} catch (IOException e1) {
e1.printStackTrace();
}
String filePath="";
if(!fileObj.isEmpty())
{
try {
System.out.println("inside this serviceImoplMethod!");
byte[] bytes = fileObj.getBytes();
String rootPath = System.getProperty("catalina.home");
File dir = new File(pathToSave);
File dir = new File(pathSave);
if (!dir.exists())
dir.mkdirs();
File serverFile = new File(dir.getAbsolutePath()
+ File.separator +fileObj.getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
filePath=serverFile.getAbsolutePath();
System.out.println("sysout file parh::" + filePath);
} catch (Exception e) {
//return "You failed to upload " + name + " => " + e.getMessage();
}
return filePath;
}
return "";
}
请帮助!!
答案 0 :(得分:0)
我认为,如果您使用java ee作为后端,并将其部署为war / jar文件并将上传的图像保存到项目文件夹中,那么您将需要重新部署项目。因为部署的java项目在其中一个文件发生更改时不会重新部署。 以防万一尝试将上传的图像保存到另一个文件夹而不是您的项目。