使用java创建客户端文件

时间:2014-01-27 09:07:14

标签: java javascript html

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

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

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

3 个答案:

答案 0 :(得分:1)

使用套接字编程,首先在服务器和客户端机器之间创建通信。   套接字kkSocket = new Socket(hostName,portNumber) 然后使用File file = new File(“hostname @ d:/file.txt”);

如果您的主机文件不包含主机名IP地址maping,则使用IP地址,而不是提供主机名。

答案 1 :(得分:1)

尽管每个人都在说,你可以通过javascript创建一个客户端文件。它是文件系统的沙盒部分,通过HTML5的FileSystem API完成。

但是,我猜你的 SECURITY_ERR 可能是因为你在浏览器中通过File://PATH_TO_HTML_PAGE打开了一个带有目标javascript的html页面。除非你从服务器中获取html / javascript / css(例如locahost:8080/test.html - 文件系统API将不起作用 - 如果你没有经验,Netbeans有一些选项可以在你的机器上本地无痛地运行glassfish /服务器实例与服务器。)。

更新1-31-2014 an article on the File-System API中找到了这一点,我确认了以上段落:

  

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

结束更新

也就是说,在之前对different question you asked and I answered的评论中,您使用的是TEMPORARY存储空间。我使用PERSISTENT因为它更可靠,浏览器会显示一条消息,要求允许在本地计算机上本地存储数据。以下是我在过去几年中在客户端计算机上本地创建文件以进行持久数据存储的方法。据我所知,这只适用于少数浏览器,我使用谷歌浏览器 - 以下无效适用于谷歌浏览器。

以下是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);
}

要记住的一件事是文件系统API是异步的,因此您必须使用回调函数。如果在实例化之前尝试访问文件系统API,或者如果在准备好之前尝试访问文件,则还会出现错误。回调函数至关重要。

答案 2 :(得分:-1)

您无法在客户端创建文件,因为浏览器不允许这样做。