【问题标题】:create client side file using java使用java创建客户端文件
【发布时间】:2014-02-18 01:09:32
【问题描述】:

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

    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”

【问题讨论】:

  • 您正在开发什么样的应用程序? B/S 还是 C/S?如果是 B/S,也许你可以通过javascript 实现这一点,但我不确定。否则,您可以开发一个客户端应用程序来在客户端上创建文件
  • @Rugal 您无法使用 JavaScript 写入文件系统。
  • 如果您希望仅在 Java 中完成此操作,则必须将创建的文件发送回作为响应,并将其保存到客户端。
  • @user1066946 我认为可能是stackedit.io 使用它将文本存储在浏览器缓存中。但是你当然不能按照你的想法创建文件,因为浏览器不允许这样做!
  • 我想创建一个 java web 应用程序来创建客户端文件或一个带有 js 的 html 应用程序来创建客户端文件

标签: java javascript html


【解决方案1】:

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

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

2014 年 1 月 31 日更新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】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      猜你喜欢
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多