java - How to upload an image to server? -


i building project spring boot , want upload image server (on local server).

code uploading file :

@controller @requestmapping("/api/v1") public class imagecontroller {      @requestmapping(value="/upload", method=requestmethod.get)     public @responsebody string provideuploadinfo() {         return "you can upload file posting same url.";     }      @requestmapping(value="/upload", method=requestmethod.post)     public @responsebody string handlefileupload(@requestparam("name") string name,@requestparam("email") string email,             @requestparam("file") multipartfile file){         if (!file.isempty()) {              bufferedimage src;             try {                 src = imageio.read(new bytearrayinputstream(file.getbytes()));              file destination = new file("/"+system.getproperty("user.home")+"/samepinchbucket/"+email+"/pics/");                   imageio.write(src, "png", destination);              //save id have used create file name in db. can retrieve image in future id.              return "you uploaded " + name + "!";             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 return "you failed upload " + name + " => " + e.getmessage();             }              }else{                  return "you failed upload " + name + " because file empty.";              }     }  } 

this code working fine image being uploaded in myfolder pics name of image requirment image should uploaded inside pics folder not in email folder.

you should use different file constructor, e.g.

string folder = "/"+system.getproperty("user.home")+ "/samepinchbucket/"+email+"/pics/"; file destination = new file(folder, file.getoriginalfilename());  

this store files inside folder pics under original filename


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -