简体   繁体   English

使用java创建客户端文件

[英]create client side file using java

i am trying to create a project which create a file in client side . 我正在尝试创建一个在客户端创建文件的项目。 i ave done the coding to create a file .but it obviously will be created in server side.. can any one help to do this. 我已经完成了编码以创建文件。但它显然将在服务器端创建..任何人都可以帮助这样做。 below is the code i have done.. 下面是我做的代码..

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

I have also tried to create a file using filesysapi, which is done using HTML and javascript. 我还尝试使用filesysapi创建一个文件,这是使用HTML和javascript完成的。 but i got "Error: SECURITY_ERR" 但我得到了“错误:SECURITY_ERR”

using socket programming, 1st create a communication between server and client machines. 使用套接字编程,首先在服务器和客户端机器之间创建通信。 Socket kkSocket = new Socket(hostName, portNumber) then use File file = new File("hostname@d:/file.txt"); 套接字kkSocket = new Socket(hostName,portNumber)然后使用File file = new File(“hostname @ d:/file.txt”);

if your host file does not contain hostname IP address maping, then instead of giving hostname, use IP address. 如果您的主机文件不包含主机名IP地址maping,则使用IP地址,而不是给主机名。

Despite what everyone is saying, you can create a client-side file via javascript. 尽管每个人都在说, 你可以通过javascript创建一个客户端文件。 It's a sandboxed portion of the File System, done via HTML5's FileSystem API. 它是文件系统的沙盒部分,通过HTML5的FileSystem API完成。

HOWEVER, my guess is your SECURITY_ERR is probably because you are opening an html page with the target javascript via File://PATH_TO_HTML_PAGE in your browser. 但是,我猜你的SECURITY_ERR可能是因为你在浏览器中通过File://PATH_TO_HTML_PAGE打开一个带有目标javascript的html页面。 The File-System API Will not work unless your grabbing the html/javascript/css from a server (like locahost:8080/test.html - Netbeans has some options to run a glassfish/server instance pretty painlessly locally on your machine if you have no experience with servers.). 除非你从服务器上获取html / javascript / css,否则文件系统API将无法工作(如locahost:8080/test.html - Netbeans有一些选项可以在你的机器上本地无痛地运行glassfish /服务器实例,如果你有没有服务器经验。

Update 1-31-2014 Found this in an article on the File-System API , which confirmed the above paragraph for me: 更新1-31-2014 在File-System API上的一篇文章中找到了这个,它为我确认了以上段落:

You may need the --allow-file-access-from-files flag if you're debugging your app from file://. 如果您正在从file://调试应用程序,则可能需要--allow-file-access-from-files标志。 Not using these flags will result in a SECURITY_ERR or QUOTA_EXCEEDED_ERR FileError. 不使用这些标志将导致SECURITY_ERR或QUOTA_EXCEEDED_ERR FileError。

end update 结束更新

That said, in the previous comment on a different question you asked and I answered , you were using TEMPORARY Storage. 也就是说,在之前对你提出另一个问题的评论中我回答说 ,你使用的是TEMPORARY Storage。 I use PERSISTENT because it is more reliable, and the browser displays a message asking for permission to store the data locally on the target machine. 我使用PERSISTENT因为它更可靠,浏览器会显示一条消息,要求允许在本地计算机上本地存储数据。 Here is how I have been making files locally on client machines for persistent data storage for the past couple years. 以下是我在过去几年中在客户端计算机上本地创建文件以进行持久数据存储的方法。 This to the best of my knowledge only works with a handful of browser's, I use Google Chrome - the following defeinitely works in Google Chrome. 据我所知,这只适用于少数浏览器,我使用谷歌浏览器 - 以下无疑适用于谷歌浏览器。

The following is javascript and needs to be within either an external script or script tags. 以下是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);
}

One thing to keep in mind is that the File-System API is asynchronous so you have to get use to using callback functions. 要记住的一件事是File-System API是异步的,因此您必须使用回调函数。 If you try to access the File-System API before it's instantiated, or if you try to access files before they are ready, you will also get errors. 如果在实例化之前尝试访问文件系统API,或者如果在准备好之前尝试访问文件,则还会出现错误。 Callback functions are essential. 回调函数至关重要。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用客户端 Java 代码上传 Spring Boot Multipart 文件 - Spring boot Multipart file upload using Client Side Java Code 使用Java和struts2在客户端下载压缩的CSV文件 - Download zipped CSV file on client side using Java and struts2 JAVA:在客户端使用select - JAVA: Using select in the client side 使用Java套接字将文件从服务器传输到客户端。 服务器端错误,文件传输到客户端为空 - File Transfer from Server to Client using java sockets. Error on Server side and File transferred to client is empty 使用Dojo文件上传客户端将其他数据发送到Java服务器端 - Send additional data to Java server-side using Dojo file upload client-side Java。 将文件保存到客户端不起作用 - Java. Save File to Client side not working 使用Java套接字的文件传输:客户端错误“线程“ main”中的异常java.lang.NullPointerException” - File transfer using java sockets : Error on client side “Exception in thread ”main“ java.lang.NullPointerException” 使用Java在客户端注册Soap处理程序 - Registering soap handler at client side using java 文件未在客户端Java(服务器/客户端)App No Web中启动 - File not launching in Client Side Java (Server/Client) App no Web 如何使用服务器端代码在客户端计算机中创建和执行批处理文件? - How do I create and execute batch file in client machine using server side code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM