我正在将图像从Javascript上传到我的服务器,我的javascript是使用Phonegap创建的应用程序的客户端部分,它的作用是拍摄画廊的图片,我希望它能在服务器上收到,但我想我有一个类型的问题,因为在客户端中,图像被编码为ImageURI,我的控制器用Java开发,Spring-MVC接收MultiPartFile,我不知道如何在MultiPartFile中转换ImageURI来接收它 . 我留下我的密码Phonegap:
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
function onPhotoURISuccess(imageURI) {
alert("Imagen URI subida");
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
alert("Imagen URI subida 2");
// Unhide image elements
//
// largeImage.style.display = 'block';
alert("Imagen URI subida 3");
// Show the captured photo
// The inline CSS rules are used to resize the image
//
// largeImage.src = imageURI;
alert("Imagen URI subida 4");
subirImagen(imageURI);
}
function subirImagen(fileURL) {
alert("SUBIR IMAGEN"+ fileURL);
var options = new FileUploadOptions();
options.fileKey = "imagen";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
var ft = new FileTransfer();
alert("UPLOAD");
ft.upload(fileURL, encodeURI("http://localhost:8080/HelloSpringMVC/fileUpload"), uploadSuccess, uploadFail, options);
}
Servidor Java Spring -MVC:@RequestMapping(value =“/ uploadFile”,method = RequestMethod.POST)public @ResponseBody String uploadFileHandler(@RequestParam(“file”)MultipartFile file){System.out.println(“RECIBO ESTO”file ); String name =“tablero.png”;的System.out.println( “HOLA”); if(!file.isEmpty()){try {byte [] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath()+ File.separator + name);
System.out.println("serverFile "+serverFile);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
return "You successfully uploaded file=" + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name
+ " because the file was empty.";
}
}