Unable to get the project directory after Springboot deployment problem:
Before I saw a question on the web when developing a Springboot project, there was a problem when the project was deployed: I exported the project as a jar package, and then when I ran with Java-jar, the file upload function in the project didn't work. Where the value of the absolute path to the directory where the file is stored is empty and the file cannot be uploaded. Problem link
It is unclear how this netizen is implemented, usually we can solve by the following solutions:
//Get the directory file Path = new File (Resourceutils.geturl ("classpath:"). GetPath ()); if (!path.exists ()) path = new File (""); System.out.println ("Path:" +path.getabsolutepath ()); If the upload directory is/static/images/upload/, you can get it as follows: File upload = new file (Path.getabsolutepath (),"static/images/ Upload/"); if (!upload.exists ()) upload.mkdirs (); System.out.println ("Upload url:" +upload.getabsolutepath ()); When developing test mode, the address is: {The project and directory}/target/static/images/upload///When packaged as a jar to be released, the address is: {Publish the jar package directory}/static/images/ upload/
It is also important to note that the above code is used because the path we store is the static directory with the jar package as it was published in the jar package, so we need to set the static resource path in the Application.properties configuration file of the jar package directory, as follows:
#设置静态资源路径,多个以逗号分隔spring.resources.static-locations=classpath:static/,file:static/
When you publish a Springboot project as a jar package, the default is to use the jar package with the application.properties in the directory as the project configuration file.
Specific Project Combat:
Resttemplate Uploading Files:
/** * Process File Upload */@RequestMapping ("/remoteupload") @ResponseBodypublic String douploadremote (httpservletrequest request, @RequestParam ("file") Multipartfile Multipartfile) {if (Multipartfile.isempty ()) {return "file is empty."; } String OriginalFilename = Multipartfile.getoriginalfilename (); String NewFileName = Uuidhelper.uuid (). Replace ("-", "") + originalfilename.substring (Originalfilename.lastindexof (".") -1); File file = null; try {file path = new File (Resourceutils.geturl ("Classpath:"). GetPath ()); File Upload = new file (Path.getabsolutepath (), "static/tmpupload/"); if (!upload.exists ()) upload.mkdirs (); String Uploadpath = upload + "\ \"; File = new File (Uploadpath + newfilename); Multipartfile.transferto (file); Submit to another service Filesystemresource remotefile = new Filesystemresource (file); Package parameter. multivaluemap<string, object> multivaluemap = new linkedmultivaluemap<>(); Multivaluemap.add ("file", remotefile); String remoteaddr = "Http://localhost:12345/test/doupload"; String res = Resttemplate.postforobject (remoteaddr, Multivaluemap, String.class); return res; } catch (Exception e) {return "File upload error."; } finally {try{file.delete (); } catch (Exception e) {//Nothing. } return "OK"; }}
You can see the resttemplate file upload:
Springmvc resttemplate File Upload
resttemplate Upload File summary
Spring Boot upload file Get project root path Physical Address resttemplate upload file