2

我的问题是我可以在我的 tomcat localhost 上写一个文件,但是当我上传到托管服务器时,我在目录中的任何地方都看不到它,也无法通过链接获取它。我收到 404 响应。

创建文件时,我使用了以下代码:

    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();
    }

}

对于路径,我使用此代码获取根路径:

      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");

我也试过用这个:

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

但它也没有用。

4

1 回答 1

0

试试这个代码而不是 FileObject 代码

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();
于 2012-12-06T09:28:08.483 回答