JAVA如何得到文件路径
发布网友
发布时间:2022-04-27 13:04
我来回答
共3个回答
热心网友
时间:2023-09-21 13:00
要得到什么文件路径``
在你要用路径的时候 比如 <img src="XXXXXXXX"></ing>
中间的XXXXXX你就这么写`先把原来的文件路径用STRING 形式存在数据库`
现在取出来`赋给变量 abc
在这里再写成
String abc = "你的文件路径"
<img src="<%=ABC%>"></ing>
热心网友
时间:2023-09-21 13:00
这个我做过
我马上给你写个例子
你等一下
写出来了,下面是代码
====================================
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author 小虫
*/
public class TestFrame extends JFrame{
static JTextField TextField;
static TestFrame testFrame;
public static void main(String args[]){
testFrame=new TestFrame();
TextField=new JTextField(" ");
JButton button=new JButton("选择文件");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//--核心代码
JFileChooser chooser = new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"文本文档", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(testFrame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
TextField.setText(chooser.getSelectedFile().getAbsolutePath());
}
//--核心代码结束
}
});
Container contentPane=testFrame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(TextField);
testFrame.setSize(300,100);
testFrame.setVisible(true);
}
}
====================================
一、JFileChooser 类为用户选择文件提供了一种简单的机制
以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
二、FileNameExtensionFilter用来指定的扩展名集合进行过滤。文件的扩展名是指文件名最后一个“.”后面的部分。
名称不包含“.”的文件没有文件扩展名。文件扩展名的比较不区分大小写。
下例创建了一个 FileNameExtensionFilter,它将显示 jpg 文件:
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
JFileChooser fileChooser = ...;
fileChooser.addChoosableFileFilter(filter);
热心网友
时间:2023-09-21 13:01
用JFileChooser
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
public class FileChooserDemo extends JPanel {
static final long serialVersionUID = 5854418136127725290L;
public class ExtensionFilter extends FileFilter {
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[]) {
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++) {
String ext = extensions[i];
if (path.endsWith(ext)
&& (path.charAt(path.length() - ext.length()) == '.')) {
return true;
}
}
return false;
}
public String getDescription() {
return (description == null ? extensions[0] : description);
}
}
public FileChooserDemo() {
JButton jb = new JButton("Open File Viewer");
add(jb);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(".");
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileFilter type1 = new ExtensionFilter("Java source", ".java");
FileFilter type2 = new ExtensionFilter("Image files",
new String[] { ".jpg", ".gif", "jpeg", "xbm" });
FileFilter type3 = new ExtensionFilter("HTML files",
new String[] { ".htm", ".html" });
chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);
chooser.addChoosableFileFilter(type3);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type2); // Initial filter setting
int status = chooser.showOpenDialog(FileChooserDemo.this);
if (status == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
System.out.println(f);
}
}
};
jb.addActionListener(listener);
}
public static void main(String args[]) {
JFrame f = new JFrame("Enhanced File Example");
JPanel j = new FileChooserDemo();
f.getContentPane().add(j, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}