从andriod我想上传图片,我正在使用Java中的Web服务

下面是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);

    } 

输出是这个我在控制台得到但文件被创建,但它已损坏,它的大小为0bytes,

--------- AndroidUploadService1518510071115内容处理:form-data; NAME = “图像”; 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注册为depandency。

@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

链接地址: http://www.djcxy.com/p/8579.html

上一篇: from andriod i am trying to upload image and i am using web services in java

下一篇: Unable to upload file from Angular client to Spring Java server: Says 400 error