简体   繁体   中英

write file on java host

My problem is that I can write a file on my tomcat localhost, but when I upload to the hosting server I don't see it anywhere in the directory and I can't get it by the link. I get a 404 response.

When create file I used this code:

    public class FileObject {
    String path;
            File uploadFile;
            public FileWriter fstream;

public void createFoler(String path){
    this.path=path;
    File newFolder = new File(path);
    try{
                if(newFolder.mkdirs()){
                    System.out.println();
                }
                else{

                }
            } catch(Exception e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(newFolder.getAbsolutePath());
}
public void createFile(String file){
        try {
    String fullpath= path + File.separator + file;
    //fstream = new FileWriter(fullpath);
            fstream = new FileWriter(file);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }catch(Exception e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

and for the path I use this code to get rootpath:

      String rootPath = request.getRealPath(this.toString()).substring(0,request.getRealPath(this.toString()).lastIndexOf(File.separator));
   FileObject fo = new FileObject();
    //        fo.createFoler( "webDatabase");
   fo.createFoler(rootPath+File.separator+ "webDatabase");
   fo.createFile("webDatabase.txt");

I alse tried to use this:

   fo.createFoler("");
   fo.createFile("webDatabase.txt");

but it didn't work either.

Try this code instead of FileObject code

File directory = new File(rootPath+File.separator+ "webDatabase");
directory.mkdirs();
File file = new File(directory, "webDatabase.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(); // You content writing here
fos.close();

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