我的问题是我可以在我的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();
}
}
对于我使用此代码获取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");
我也尝试过这个:
fo.createFoler("");
fo.createFile("webDatabase.txt");
但它也没有用。
答案 0 :(得分: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();