使用java创建客户端文件

ess*_*ess 6 html javascript java

我正在尝试创建一个在客户端创建文件的项目.我已经完成了编码以创建文件.但它显然将在服务器端创建..任何人都可以帮助这样做.下面是我做的代码..

    File file = new File("d:/file.txt");
        try {

            String content = "This is the content to write into file";
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

我还尝试使用filesysapi创建一个文件,这是使用HTML和javascript完成的.但我得到了"错误:SECURITY_ERR"

Art*_*org 1

不管大家怎么说,您都可以通过 JavaScript 创建客户端文件。它是文件系统的沙盒部分,通过 HTML5 的文件系统 API 完成。

然而,我的猜测是你的SECURITY_ERRFile://PATH_TO_HTML_PAGE可能是因为你正在浏览器中使用目标 javascript 打开一个 html 页面。除非您从服务器获取 html/javascript/css,否则文件系统 API 将无法工作(例如locahost:8080/test.html,如果您没有服务器经验,Netbeans 有一些选项可以在您的计算机上轻松地在本地运行 glassfish/服务器实例。)。

更新 1-31-2014在一篇关于 File-System API 的文章 中找到了这一点,它为我证实了上面的段落:

如果您从 file:// 调试应用程序,则可能需要 --allow-file-access-from-files 标志。不使用这些标志将导致 SECURITY_ERR 或 QUOTA_EXCEEDED_ERR 文件错误。

结束更新

也就是说,在之前对您提出且我回答的另一个问题的评论中,您正在使用TEMPORARY存储。我使用PERSISTENT它是因为它更可靠,并且浏览器会显示一条消息,请求允许将数据本地存储在目标计算机上。以下是过去几年我在客户端计算机上本地创建文件以进行持久数据存储的方法。据我所知,这仅适用于少数浏览器,我使用 Google Chrome - 以下内容肯定适用于 Google Chrome。

以下是 javascript,需要位于外部脚本或script标签内。

//this is a callback function that gets passed to your request for the file-System.
var onInitFs = function(fileSys){
    //fileSystem is a global variable
    fileSystem = fileSys;
    //once you have access to the fileSystem api, then you can create a file locally
    makeAFile();
    makeAndWriteContent();
};
var errorHandler = function(e){console.log('Error', e);};

//request 1 GB memory in a quota request
//note the internal callback `function(grantedBytes){...}` which makes the actual 
//request for the Filesystem, on success `onInitFs` is called. 
///on error the `errorHandler` is called
navigator.webkitPersistentStorage.requestQuota(1024*1024*1024*1, function(grantedBytes) {
    window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler); 
}, errorHandler);

//this method will only work once the fileSystem variable has been initialized
function makeAFile(){
    var callbackFunctionOnSuccess = function(){console.log("created new file")}
    fileSystem.root.getFile("test.txt", {
        create: true
    }, callbackFunctionOnSuccess, function(error){console.log(error);});
}

function makeAndWriteContent(){
    //this is going to be passed as a callback function, to be executed after
    //contents are written to the test2.txt file.
    var readFile = function(){
       fileSystem.root.getFile("test2.txt", {create: false}, function(fileEntry) {
           fileEntry.file(function(file) {
              var reader = new FileReader();
              reader.onloadend = function(e) {
                console.log(this.result);
              };
              reader.readAsText(file);
           }, function(error){console.log(error);});
         }, function(error){console.log(error);});
    }


    fileSystem.root.getFile("test2.txt", {
        create: true
    }, function(fileEntry) {
        fileEntry.createWriter(function(writer) {
            writer.onwriteend = function(e) {
                writer.onwriteend = function(e){
                    //now, we will read back what we wrote.
                    readFile();
                }
                writer.onerror = function(e3){console.log(e3);
                }
                var blob = new Blob(["Hello World"]);
                writer.write(blob);
            };
            writer.onerror = function(e3) {console.log(e3);};
            //make sure our target file is empty before writing to it.
            writer.truncate(0);
        }, errorHandler);
    }, errorHandler);
}
Run Code Online (Sandbox Code Playgroud)

需要记住的一件事是文件系统 API 是异步的,因此您必须习惯使用回调函数。如果您尝试在实例化文件系统 API 之前访问它,或者尝试在文件准备好之前访问文件,您也会收到错误。回调函数是必不可少的。