将图像从Android应用程序上传到Spring服务器

时间:2018-03-29 13:45:00

标签: java android spring spring-mvc image-uploading

我将图像从Android应用程序上传到spring服务器时出现问题。首先,我在我的服务器上有这个方法:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String fileUpload(@RequestBody MultipartFile file) {
    System.out.println("post image");
    try {
        // Get the file and save it somewhere
        byte[] bytes = file.getBytes();
        System.out.println(bytes.length);

        //save file in server - you may need an another scenario
        Path path = Paths.get("file:///E:/together/images/" + file.getOriginalFilename());
        Files.write(path, bytes);

    } catch (IOException e) {
        e.printStackTrace();
    }

    //redirect to an another url end point
    return "redirect";
}

在我的Android应用程序中,我可以从图库中获取图像,并尝试使用post post方法

FileEntity fileEntity = new FileEntity(new File(getRealPathFromURI(uri)), "application/octet-stream");
                        new Requests.Async().execute(fileEntity);
ublic static class Async extends AsyncTask<FileEntity,Void,Void>{

    @Override
    protected void onPreExecute() {
        restTemplate = RestTemplateSingleton.newInstance().getRestTemplate();
    }

    @Override
    protected Void doInBackground(FileEntity... uris) {
        try{
            restTemplate.postForObject(Requests.insertImage(),uris[0],FileEntity.class);
        }catch (HttpClientErrorException | HttpServerErrorException httpClientOrServerExc) {
            return null;
        }
        return null;
    }
}

但是在调用此方法时我得到500 http状态。什么是我的错?

0 个答案:

没有答案