简体   繁体   中英

Can't save file in tomcat webapp running on a linux server

Im currently debugging a webapp , where the user is supposed to upload a file which is then stored temporarily on the server and send via E-Mail to an administrator.

The app runs on a tomcat 8 in a linux server environment. The class from where the error occurs looks like follows:

@Service
public class BugReportBo implements IBugReportBo {

  ...

  @Value("${app.temp.folder}")
  private Resource temp;

  ...

  private File byteArrayToFile(final byte[] lob, final String string) throws IOException {
    final int locatorStartPos = 6;
    String uri = temp.getURI().toString().substring(locatorStartPos);

    //uri: var/lib/tomcat/webapps/prototype/res/temp/
    //string: temp0.jpg

    // convert array of bytes into file
    File f = new File(uri + string);
    FileOutputStream fileOuputStream = new FileOutputStream(uri + string);
    fileOuputStream.write(lob);
    fileOuputStream.close();
    return f;
  }
}

The error is thrown from FileOutputStream fileOuputStream = new FileOutputStream(uri + string); .

The Exception is a FileNotFoundException (message:"var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg (Datei oder Verzeichnis nicht gefunden)").

Everything seems to be ok to me as the folder is where it is supposed to be and the path seems to be fine as well.

Any ideas?

var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg

You forget the first '/', so the path is understand like a relative path, instead of an absolute path.

/var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM