1

Last 3 days I started to learn Spring. I want to send a image from the phone gallery to the spring server. I want to mention that the server is local so I'm using localhost. I saw a tutorial that if I want to send stuff to a local server, the server address is my laptop address + the port (ex. 8080) and I have to connect the phone to the same Wi-Fi as the laptop.

I know how to get the image from the gallery but I don't know how to send it. Many solutions from stackoverflow are old and some classes got deprecated and I can't try their method.

Also, what should I do in the spring controller to receive the image?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ProgIsMetal
  • 462
  • 2
  • 5
  • 18
  • 1
    You did not read any stackiverflow page tagged android! Every day sombody asks how to post a file. And every day it is solved. Please read! – greenapps Mar 07 '17 at 09:26
  • 1
    @greenapps *Many solutions from stackoverflow are old and some classes got deprecated* -- True statement. `HttpClient` answers are all over the place. – OneCricketeer Mar 07 '17 at 10:06

1 Answers1

1

You'll have use MultipartFile to upload an image using spring. Please go through following example.

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

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

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

    //redirect to an another url end point 
    return "redirect:/upload-status";
}

Please make sure you can reach to your computer through your mobile device. I believe you may know that Android requires additional privilege to use network connections. So make sure you have permitted your app to access network.

EDIT:

You can use HttpClient to upload file from your mobile app. Please try following code.

    HttpClient httpClient = AndroidHttpClient.newInstance("App");
    HttpPost httpPost = new HttpPost("http://your-server-url");

    httpPost.setEntity(new FileEntity(new File("your-file-path"), "application/octet-stream"));

    HttpResponse response = httpClient.execute(httpPost);
Channa Jayamuni
  • 1,876
  • 1
  • 18
  • 29
  • 1
    Hello Channa Jayamuni! How do I send the image from the android app? I saw people using HttpUrlConnection. – ProgIsMetal Mar 07 '17 at 09:57
  • `HttpClient` is deprecated, so please mention how to get that class. – OneCricketeer Mar 07 '17 at 10:07
  • Since HttpClient is deprecated, you can you `HttpURLConnetion` to send file data to the server. I found it on following reference. Please be kind enough to use following alternative way. http://stackoverflow.com/questions/34915556/post-a-file-with-other-form-data-using-httpurlconnection – Channa Jayamuni Mar 07 '17 at 10:18
  • So that s it? I give the path of the image from the gallery? The file sent has the picture size(4-6MB)? I don't have to convert it in base64 or something else and the server to decompress it? – ProgIsMetal Mar 07 '17 at 10:32