使用java创建客户端文件 [英] create client side file using java
问题描述
我正在尝试创建一个在客户端创建文件的项目。我已经完成了编码创建一个文件,但它显然将在服务器端创建..任何人都可以帮助做到这一点。下面是我已经完成的代码。
File file = new File(d:/file.txt);
尝试{
字符串内容=这是要写入文件的内容;
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println(完成);
} catch(IOException e){
e.printStackTrace();
}
我也尝试使用filesysapi创建一个文件,和JavaScript。但我得到了Error:SECURITY_ERR
尽管大家都在说,你可以创建一个客户端文件通过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
在关于文件系统API的文章中发现了这一点,它为我确认了上面的段落:
如果您正在调试,您可能需要--allow-file-access-from-files标志你的应用程序来自file://。不使用这些标志将导致SECURITY_ERR或QUOTA_EXCEEDED_ERR FileError。
结束更新
// this是一个回调函数,传递给您对文件系统的请求。
var onInitFs = function(fileSys){
// fileSystem是一个全局变量
fileSystem = fileSys;
//一旦你有权访问fileSystem api,那么你可以在本地创建一个文件
makeAFile();
makeAndWriteContent();
};
var errorHandler = function(e){console.log('Error',e);};
//在配额请求中请求1 GB内存
//请注意内部回调函数(grantedBytes){...}`这使得实际的
//请求对于Filesystem,成功调用`onInitFs`。
///错误errorHandler被称为
navigator.webkitPersistentStorage.requestQuota(1024 * 1024 * 1024 * 1,function(grantedBytes){
window.webkitRequestFileSystem(PERSISTENT,grantedBytes, onInitFs,errorHandler);
},errorHandler);
//这个方法只有在fileSystem变量被初始化后才会起作用
函数makeAFile(){
var callbackFunctionOnSuccess = function(){console.log(created new file )}
fileSystem.root.getFile(test.txt,{
create:true
},callbackFunctionOnSuccess,function(error){console.log(error);});
}
函数makeAndWriteContent(){
//这将作为回调函数传递,在
// //内容被写入test2.txt文件。
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);});
$ b fileSystem.root.getFile(test2.txt,{
create:true
},function(fileEntry){
fileEntry.createWriter(function(writer){
writer.onwriteend = function(e){
writer.onwriteend = function(e){
//现在,我们将读回我们写了:
readFile();
}
writer.onerror = function(e3){console.log(e3);
}
var blob = new Blob ([Hello World]);
writer.write(blob);
};
writer.onerror = function(e3){console.log(e3);};
//在写入之前确保我们的目标文件是空的
writer.truncate(0);
},errorHandler);
},errorHandler);
}
请注意一点,File-System API是异步的,你必须习惯使用回调函数。如果您尝试在实例化之前访问文件系统API,或者如果您尝试在文件准备好之前访问它们,您也将遇到错误。回调函数是必不可少的。
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. but i got "Error: SECURITY_ERR"
Despite what everyone is saying, you can create a client-side file via javascript. It's a sandboxed portion of the File System, done via HTML5's 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. 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.).
Update 1-31-2014 Found this in an article on the File-System API, which confirmed the above paragraph for me:
You may need the --allow-file-access-from-files flag if you're debugging your app from file://. Not using these flags will result in a SECURITY_ERR or 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. 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. 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.
//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. 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. Callback functions are essential.
这篇关于使用java创建客户端文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!