来自 andriod,我正在尝试上传图片,我正在 java 中使用网络服务
from andriod i am trying to upload image and i am using web services in java
下面是android部分
new MultipartUploadRequest(this,uploadid,UPLOAD_URL)
.addFileToUpload(path,"image")
.addParameter("name",name)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
下面是我在 Web 服务中的 java 控制器
@RequestMapping(value = "/uploadm",method=RequestMethod.POST)
public void submitQuestionuploading(@RequestBody String image) throws Exception
{
System.out.println(1+""+image);
try {
byte[] bytes = image.getBytes();
System.out.println(11);
BufferedOutputStream stream =new BufferedOutputStream(new
FileOutputStream(new File(UPLOAD_DIRECTORY +"11.png")));
stream.write(bytes);
stream.flush();
stream.close();
}
catch (Exception e) {
System.out.println(e);
}
输出是我在控制台中得到的,但文件已创建但已损坏且大小为 0 字节,
---------AndroidUploadService1518510071115 Content-Disposition: form-data; name="image"; filename="IMG_20180211_000033.jpg"
Content-Type: image/jpeg
ÿØÿá3ØExif
我试着把它放在 java 控制器中,但它不起作用
@RequestMapping(value = "/upload", method = RequestMethod.POST ,
headers = "Content-Type=multipart/form-data") public String
fileUpload(@RequestParam("image") CommonsMultipartFile file) {}
但我只想在spring MVC
中做,帮我把上传的文件
这是一个有效的文件上传器
@ResponseStatus(code = HttpStatus.CREATED)
@RequestMapping(value = "asset", method = RequestMethod.POST, consumes = {
MediaType.MULTIPART_FORM_DATA_VALUE}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String uploadImage(
@RequestParam("image") MultipartFile file) {
byte[] bytes = file.getBytes();
//do something with byte
return "ok or anything you want to return";
}
而且你还需要将 MultipartResolver 注册为依赖。
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
您可以部署此代码,然后使用邮递员进行测试。
对此有各种教程。
你可以看看
http://www.baeldung.com/spring-file-upload
https://www.boraji.com/spring-4-mvc-file-upload-example-with-commons-fileupload
下面是android部分
new MultipartUploadRequest(this,uploadid,UPLOAD_URL)
.addFileToUpload(path,"image")
.addParameter("name",name)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
下面是我在 Web 服务中的 java 控制器
@RequestMapping(value = "/uploadm",method=RequestMethod.POST)
public void submitQuestionuploading(@RequestBody String image) throws Exception
{
System.out.println(1+""+image);
try {
byte[] bytes = image.getBytes();
System.out.println(11);
BufferedOutputStream stream =new BufferedOutputStream(new
FileOutputStream(new File(UPLOAD_DIRECTORY +"11.png")));
stream.write(bytes);
stream.flush();
stream.close();
}
catch (Exception e) {
System.out.println(e);
}
输出是我在控制台中得到的,但文件已创建但已损坏且大小为 0 字节,
---------AndroidUploadService1518510071115 Content-Disposition: form-data; name="image"; filename="IMG_20180211_000033.jpg" Content-Type: image/jpeg
ÿØÿá3ØExif
我试着把它放在 java 控制器中,但它不起作用
@RequestMapping(value = "/upload", method = RequestMethod.POST , headers = "Content-Type=multipart/form-data") public String fileUpload(@RequestParam("image") CommonsMultipartFile file) {}
但我只想在spring MVC
中做,帮我把上传的文件
这是一个有效的文件上传器
@ResponseStatus(code = HttpStatus.CREATED)
@RequestMapping(value = "asset", method = RequestMethod.POST, consumes = {
MediaType.MULTIPART_FORM_DATA_VALUE}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String uploadImage(
@RequestParam("image") MultipartFile file) {
byte[] bytes = file.getBytes();
//do something with byte
return "ok or anything you want to return";
}
而且你还需要将 MultipartResolver 注册为依赖。
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
您可以部署此代码,然后使用邮递员进行测试。
对此有各种教程。 你可以看看
http://www.baeldung.com/spring-file-upload
https://www.boraji.com/spring-4-mvc-file-upload-example-with-commons-fileupload