Use the current relative path or absolute path as the parameter when creating a fileinputstream

Source: Internet
Author: User

Today, I am writing a program for transmitting images through the mobile phone and PC WebService (implemented using axis2). The server is a class and is directly deployed in the pojo folder of axis after compilation, when I create a new fileinputstream, I want to use a relative path so that no matter where my server is deployed, I can always use a folder without having to modify the program path code, of course, first of all, I use absolute paths for experiments to ensure successful communication. When using absolute paths, note that the path separator can be "/" or "\". this can also be mixed as follows:

Fileinputstream FCM = new fileinputstream ("D:/images/" + ID + ". jpg ");
Or
Fileinputstream FCM = new fileinputstream ("D: \ images \" + ID + ". jpg ");
Yes.
It can even be used in combination.

Then I want to use the relative path, but when I compare the images folder with my. when the class file (WebService to be executed) is placed in the same directory, it is found that the operation failed and the folder cannot be found. The Code is as follows:

Fileinputstream FCM = new fileinputstream ("images/" + ID + ". jpg ");

Then I printed the current directory using system. Out. println (new file ("."). getabsolutepath ();

The result is the bin directory of Tomcat, instead of the WebService directory of Tomcat, which is modified as follows:
Fileinputstream FCM = new fileinputstream ("../webapps \ axis2 \ WEB-INF/pojo/images/" + ID + ". jpg ");

Note: ../indicates the upper-level directory of the current directory.

Therefore, if you want to use the current directory when deploying it as a WebService to Tomcat, you 'd better use system first. out. println (new file (". "). getabsolutepath (); check what the current directory is, because it is not necessarily yours. the directory where class files are stored, usually

Tomcat bin directory, so you need to re-specify the directory you want to reach based on this directory.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.