这是我的JSP文件.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
//JFileChooser filechoose = new JFileChooser();
JFileChooser filechoose = new JFileChooser("D:\\");
filechoose.showOpenDialog(null);
File file = filechoose.getSelectedFile();
XLCauHoi.ImportXmlFileToData(file);
%>
<h4> ?ã xu?t file thành công </h4>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的问题是:JFileChooser
当我在浏览器上运行时弹出2次.如果我在Java类中运行它,会JFileChooser
弹出一次.我的问题是什么以及如何解决?
在我的Java应用程序中,我需要使用JFileChooser选择路径.我写的代码如下:
jfChooser = new JFileChooser();
jfChooser.setCurrentDirectory(new java.io.File("."));
jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "+ jfChooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "+ jfChooser.getSelectedFile());
tfPath.setText(jfChooser.getSelectedFile().getAbsolutePath()); // the selected path set to textfield which is lated get by the program
}
else {
System.out.println("No Selection ");
}
Run Code Online (Sandbox Code Playgroud)
我正在获得正确的路径.例如,这里我得到的路径(在Windows操作系统中)
String choosedPath=tfPath.getText().trimm();
Run Code Online (Sandbox Code Playgroud)
现在实际上我想以编程方式在给定路径(即newfolder目录中)上创建另一个目录.
为此,我有新的目录名称"newdir",因此传递给File构造函数以创建此目录的字符串如下所示:
File createFolder = new File("choosedPath"+"\\"+"newdir");
Run Code Online (Sandbox Code Playgroud)
现在问题是我的应用程序可能在Windows上运行或者可能在Linux上运行,因此文件路径分隔符会有所不同(例如,对于Windows为'/',对于linux为'\')
我如何克服这个问题,以便根据操作系统获得路径中的propper斜杠?
我有一个使用JFileChooser的程序.简而言之,完整的程序是一个GUI,允许用户操纵PNG和JPG.我想这样做,以便JFileChooser立即打开图片目录(窗口).当用户打开他们的JFileChooser时,它会直接打开图片库C:\ Users \(USER)\ Pictures
此外,仅显示特定类型的文件(PNG和JPG)会很好.许多程序似乎能够做到这一点; 只允许选择特定文件.JFileChooser是否允许这样的事情?目前,我正在使用一种大规模不可靠的运行方法来拒绝非PNG/JPG.
以下是GUI的"浏览"按钮,用户将选择其图片进行编辑,并将其显示在屏幕上.
try {
int val = filec.showOpenDialog(GridCreator.this);
if(val==JFileChooser.APPROVE_OPTION) {
File unfiltered_picture = filec.getSelectedFile();
//get the extension of the file
extension=unfiltered_picture.getPath();
int index=extension.indexOf(".");
extension=extension.substring(index+1, extension.length());
//if the file is not jpg, png, or jpeg, reject it and send a message to the user.
if(!extension.matches("[jJ][pP][gG]") && !extension.matches("[pP][nN][gG]") && !extension.matches("[jJ][pP][eE][gG]")) {
JOptionPane.showMessageDialog(null,
"cannot load file. File must be of type png, jpeg, or jpg. \n Your file is of type " + extension,
"Error: improper file",
JOptionPane.OK_OPTION); …
Run Code Online (Sandbox Code Playgroud) 我有一个方法fileUpload()打开FileChooser-Menu.如果我选择一个文件,那么绝对路径和文件大小写在控制台上.首先我只尝试这个:
System.out.println(file.getAbsolutePath()); // Print: C:\Users\Anonym XY\Desktop\test.txt\C:\Users\Anonym XY\Desktop\test.txt
System.out.println(file.length()); // Print: "0"
Run Code Online (Sandbox Code Playgroud)
但是对于绝对路径,我得到绝对路径,但双重打印?!Oo和文件大小我到这里0,这是假大小.
如果我这样:
System.out.println(fileChooser.getSelectedFile().length()); // Print: "15747840"
Run Code Online (Sandbox Code Playgroud)
然后我得到正确的文件大小.
我的整个方法:
public void fileUpload() {
JFileChooser fileChooser = new JFileChooser();
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
File file = new File(fileChooser.getSelectedFile() ,fileChooser.getSelectedFile().getAbsolutePath());
System.out.println("Path: " + file.getAbsolutePath()); // Print: "Path: C:\Users\Anonym XY\Desktop\test.txt\C:\Users\Anonym XY\Desktop\test.txt"
System.out.println("Filesize: " + fileChooser.getSelectedFile().length()); // Print: "Filesize: 15747840"
System.out.println("Filesize: " + file.length()); // Print: "Filesize: 0"
}else if(fileChooser.showOpenDialog(null)==JFileChooser.ERROR_OPTION) {
System.out.println("Error");
}
}
Run Code Online (Sandbox Code Playgroud) 我想在Java中使用JFileChooser来让用户选择他们想要保存文件的位置.我用过这段代码:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
这有效并且确实获得了路径,但是对话框要求用户选择文件以获取文件路径.因为我希望它保存一个新文件,我需要它来获取路径,而不必选择文件,而是从用户选择的文件夹.
我是新手,不知道有任何其他方式这样做,请你告诉我一个方法.
我正在尝试返回用户选择的文件.这很好.我可以在openFile中检查fileToOpen并且它是100%正确的,但是当我在main方法中sysout它时,我只是得到null.我想要用户选择的路径.
这是主要类:
public class Main {
public static void main(String[] args) {
File fileToOpen = null;
ReadIn openLog = new ReadIn();
openLog.openFile(fileToOpen);
System.out.println(fileToOpen);
}
}
Run Code Online (Sandbox Code Playgroud)
这是ReadIn类:
public class ReadIn extends JFrame{
public File openFile(File fileToOpen){
final JFileChooser fileChooser = new JFileChooser();
int modalToComponent=fileChooser.showOpenDialog(this);
if (modalToComponent == JFileChooser.APPROVE_OPTION) {
fileToOpen = fileChooser.getSelectedFile();
}
return fileToOpen;
}
}
Run Code Online (Sandbox Code Playgroud) final JFileChooser fc = new JFileChooser();
File[] files = fc.getSelectedFiles();
private void showTxtFileFrame() {
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fc.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION) {
textfield1.setText(fc.getSelectedFile().getAbsolutePath());
}
Run Code Online (Sandbox Code Playgroud)
我想选择多个文件并将它们列在我的文本字段中。我可以选择多个文件,但它只显示单个文件的绝对路径。
我正在制作一个桌面应用程序,它有一个JFileChooser(ShowSaveDialog)函数..当我试图保存一个示例文本文件时,程序没有得到我选择的扩展文件..我正在尝试使用if else或switch语句和我无法弄清楚如果选择pdf,word或txt扩展名作为文件扩展名,我将使用什么命令来获取条件的字符串/ Int值...
public class Save {
static boolean flag = false;
public static void main(String[] args) throws IOException, SQLException {
JFileChooser saveFile = new JFileChooser();
saveFile.setDialogTitle("Save as");
FileNameExtensionFilter File_ext_txt =
new FileNameExtensionFilter("Text Documents(*.txt)", "txt");
FileNameExtensionFilter File_ext_pdf =
new FileNameExtensionFilter("PDF", "pdf");
FileNameExtensionFilter File_ext_doc =
new FileNameExtensionFilter("Word 97-2003 Document", "doc");
saveFile.addChoosableFileFilter(File_ext_pdf);
saveFile.addChoosableFileFilter(File_ext_doc);
saveFile.addChoosableFileFilter(File_ext_txt);
FileFilter extension = saveFile.getFileFilter();
int userSelection = saveFile.showSaveDialog(null);
File File_Path = saveFile.getSelectedFile();
String fullPath = File_Path.getAbsolutePath();
String Ext = null;
if (userSelection == JFileChooser.APPROVE_OPTION){
if(extension == File_ext_txt){
Ext …
Run Code Online (Sandbox Code Playgroud) 你好,我在一个java项目中工作,我想创建一个我拥有的JFileChooser,但我只想在一个目录中选择,就像我有一个名为“Saves”的目录,我希望打开JFileChooser的人只能访问其中的txt目录调用“保存”。他们无法访问其他任何内容,只能访问我设置的目录。
我研究了很多,但找不到任何东西。如果您知道其他类似的选择,请帮助我!谢谢。