Java 浏览按钮选择目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18332506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Browse button to select directory
提问by pise
I want to create a browse button in my web page to select directory and not file. I know that input type file won't work here but is there any way to do it with Javascript. I want to get the filepath of client machine which is possible in IE but other browser are not supporting but that is fine for me.
我想在我的网页中创建一个浏览按钮来选择目录而不是文件。我知道输入类型文件在这里不起作用,但是有什么方法可以用 Javascript 来做到这一点。我想获取客户端机器的文件路径,这在 IE 中是可能的,但其他浏览器不支持,但这对我来说很好。
The way I got stuck is how to get file directory in button.
我卡住的方式是如何在按钮中获取文件目录。
Below is the code I am using to call applet from browser but I am getting Detected from bootclasspath: C:\PROGRA~1\Java\jre7\lib\deploy.jar error in browser. I have compiled class file using Java 1.5
下面是我用来从浏览器调用小程序的代码,但我从引导类路径中检测到:C:\PROGRA~1\Java\jre7\lib\deploy.jar 在浏览器中出错。我已经使用 Java 1.5 编译了类文件
<applet code="com.life.draw.BrowsePage.class"></applet>
Code
代码
public class BrowsePage extends JApplet {
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Browse the folder to process");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
}
}
采纳答案by MadProgrammer
Why the hell are you calling this in the your paint
method? This is likely trying creating to create new windows EVERY TIME the applet is painted
.
你到底为什么在你的paint
方法中调用它?这可能是每次小程序出现时都试图创建新窗口painted
。
public void paint(Graphics g) {
// TODO Auto-generated method stub
JFileChooser chooser = new JFileChooser();
/*...*/
Instead, create a JButton
in your init
method and attach an ActionListener
to it...
相反,JButton
在您的init
方法中创建一个并将其附加ActionListener
到...
public void init() {
setLayout(new GridBagLayout());
JButton browse = new JButton("...");
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Browse the folder to process");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
}
});
add(browse);
}
You might also like to take a look at What Applets Can and Cannot Do
您可能还想看看小程序可以做什么和不能做什么
回答by Spikeh
The only way you can get a local browse dialogue in a web browser is either by using <input type="file"/>
, or by using a Java Applet or Adobe Flash plugin. There is no built in way to get a directory reference from JS in a web browser.
在 Web 浏览器中获得本地浏览对话的唯一方法是使用<input type="file"/>
,或者使用 Java Applet 或 Adobe Flash 插件。没有内置的方法可以在 Web 浏览器中从 JS 获取目录引用。
Also, you cannot read the contents of a client's hard disk, or even initiate a browse dialogue via JavaScript. If you were able to, it would impose considerable security issues.
此外,您无法读取客户端硬盘的内容,甚至无法通过 JavaScript 启动浏览对话。如果您能够这样做,它将带来相当大的安全问题。
In reference to reading a directory, take a look at the following posts:
关于阅读目录,请查看以下帖子:
Local file access with javascript
Getting content of a local file without uploading
Javascript: Getting the contents of a local server-side file
By the sound of it, you're going to need to write a flash plugin that lets you select a directory locally. Your users will be given a security warning when downloading the plugin, though.
听上去,您将需要编写一个 Flash 插件,让您可以在本地选择一个目录。不过,您的用户在下载插件时会收到安全警告。
Edit:
编辑:
There's also the webkit based method, but this will only work in webkit based browsers (Chrome, Safari etc).
还有基于 webkit 的方法,但这仅适用于基于 webkit 的浏览器(Chrome、Safari 等)。
How do I use Google Chrome 11's Upload Folder feature in my own code?