Create client file using java

I am trying to create a project that creates a file on the client side. I made an encoding to create the file. But it will obviously be created on the server side. Can anyone help to do this. Below is the code I made.

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 also tried to create a file using filesysapi, which is executed using HTML and javascript. but I got "Error: SECURITY_ERR"

+6
source share
3 answers

Despite what everyone says, you can create a client file through javascript. This is an isolated part of the file system created using the HTML5 FileSystem APIs.

HOWEVER, I think your SECURITY_ERR is probably because you open the html page with the target javascript via File://PATH_TO_HTML_PAGE in your browser. The file system API will not work unless you grab html / javascript / css from the server (e.g. locahost:8080/test.html ). Netbeans has some options for launching an instance of Glassfish / server rather painlessly locally on your computer if you have no experience with servers.).

Update 1-31-2014 Found this in an article on the file system API that confirmed my paragraph above:

You may need the flag --allow-file-access-from-files if you are debugging your application from the file: //. Not using these flags will result in a SECURITY_ERR or QUOTA_EXCEEDED_ERR FileError.

end of update

In the previous comment on another question that you asked, and I answered , you used TEMPORARY Storage. I use PERSISTENT because it is more reliable, and the browser displays a message asking you to allow data to be stored locally on the target machine. This is how I made files locally on client computers to permanently store data over the last two years. This, as far as I know, works only with a few browsers, I use Google Chrome - it works several times in Google Chrome.

The following is javascript and should be within the outer tags of a script or 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); } 

Keep in mind that the file system API is asynchronous, so you need to use callback functions. If you try to access the file system API before it is created, or if you try to access the files before they are ready, you will also get errors. Callback functions are required.

+1
source

using socket programming, 1st create a connection between the server and the client machines. Socket kkSocket = new Socket (hostName, portNumber) then use File file = new File ("hostname @d: /file.txt");

if your host file does not contain an IP address mapping of the host name, use the IP address instead of specifying the host name.

+1
source

You cannot create files on the client side, because the browser does not allow this.

-1
source

All Articles