java d出选择目录框(选择文件夹),获取选择的文件夹路径:
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemViewgetFileSystemView(); //注意了,这里重要的一句
Systemoutprintln(fsvgetHomeDirectory()); //得到桌面路径
fileChoosersetCurrentDirectory(fsvgetHomeDirectory());
fileChoosersetDialogTitle("请选择要上传的文件");
fileChoosersetApproveButtonText("确定");
fileChoosersetFileSelectionMode(JFileChooserFILES_ONLY);
result = fileChoosershowOpenDialog(chatFrame);
if (JFileChooserAPPROVE_OPTION == result) {
path=fileChoosergetSelectedFile()getPath();
Systemoutprintln("path: "+path);
}
这是另外一种方法得到桌面路径:
File desktop = new File(SystemgetProperty("userhome")+SystemgetProperty("fileseparator")+"XX");
filechoosersetCurrentDirectory(desktop);
我的文档 路径: fsvgetDefaultDirectory());
username 用户的账户名称
userhome 用户的主目录
userdir 用户的当前工作目录
javaversion Java 运行时环境版本
javavendor Java 运行时环境供应商
javavendorurl Java 供应商的 URL
javahome Java 安装目录
javavmspecificationversion Java 虚拟机规范版本
javavmspecificationvendor Java 虚拟机规范供应商
javavmspecificationname Java 虚拟机规范名称
javavmversion Java 虚拟机实现版本
javavmvendor Java 虚拟机实现供应商
javavmname Java 虚拟机实现名称
javaspecificationversion Java 运行时环境规范版本
javaspecificationvendor Java 运行时环境规范供应商
javaspecificationname Java 运行时环境规范名称
javaclassversion Java 类格式版本号
javaclasspath Java 类路径
javalibrarypath 加载库时搜索的路径列表
javaiotmpdir 默认的临时文件路径
javacompiler 要使用的 JIT 编译器的名称
javaextdirs 一个或多个扩展目录的路径
osname *** 作系统的名称
osarch *** 作系统的架构
osversion *** 作系统的版本
import javaioFile;
import javaxswingfilechooserFileSystemView;
public class Test {
public static void main(String[] args) {
FileSystemView fsv=FileSystemViewgetFileSystemView();
//将桌面的那个文件目录赋值给file
File file=fsvgetHomeDirectory();
//输出桌面那个目录的路径
Systemoutprintln(filegetPath());
}
}
你好,提问者:
指定资源路径的方法有两种:
一种是绝对路径,一种是相对路径。
获取当前类的所在工程路径;File f = new File(thisgetClass()getResource("/")getPath());
Systemoutprintln(f);
获取当前类的绝对路径;
File f = new File(thisgetClass()getResource("")getPath());
Systemoutprintln(f);
获取当前类的所在工程路径;
File directory = new File("");//参数为空
String courseFile = directorygetCanonicalPath() ;
Systemoutprintln(courseFile);
获取当前工程src目录下selectedtxt文件的路径:
URL xmlpath = thisgetClass()getClassLoader()getResource("selectedtxt");
Systemoutprintln(xmlpath);
//上下文路径
String contextPath = requestgetContextPath();
//URI路径
String basePath = requestgetScheme() + "://"
+ requestgetServerName() + ":" + requestgetServerPort()
+ contextPath + "/";
使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:
1、使用JSP Servlet取得WEB根路径可以用requestgetContextPath(),相对路径requestgetSession()getServletContext()getRealPath("/"),它们可以使用我们很容易取得根路径。
2、如果使用了spring, 在WEB-INF/webxml中,创建一个webAppRootKey的param,指定一个值(默认为webapproot)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext()getRealPath("/"); 并将webAppRootKey对应的webapproot分别作为Key,Value写到System Properties系统属性中。之后在程序中通过SystemgetProperty("webapproot")来获得WebRoot的物理路径。
具体示例代码如下:
webxml
<xml version="10" encoding="UTF-8">
<web-app version="24"
xmlns=">
一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 Fileseparator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中, 如何用绝对路径访问呢?
现在建立一个目录:
File fDir=new File(Fileseparator); //Fileseparator表示根目录,比如现在就表示在D盘下。
String strFile="java"+Fileseparator+"src"+Fileseparator+"myjava"; //这个就是绝对路径
File f=new File(fDir,strFile);
拿去用吧。
package comweixinutil;import javaioFile;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javaioPrintWriter;
import javaioRandomAccessFile;
import orgapachecommonsnetPrintCommandListener;
import orgapachecommonsnetftpFTP;
import orgapachecommonsnetftpFTPClient;
import orgapachecommonsnetftpFTPFile;
import orgapachecommonsnetftpFTPReply;
import comweixinconstantDownloadStatus;
import comweixinconstantUploadStatus;
/
支持断点续传的FTP实用类
@version 01 实现基本断点上传下载
@version 02 实现上传下载进度汇报
@version 03 实现中文目录创建及中文文件创建,添加对于中文的支持
/
public class ContinueFTP {
public FTPClient ftpClient = new FTPClient();
public ContinueFTP(){
//设置将过程中使用到的命令输出到控制台
thisftpClientaddProtocolCommandListener(new PrintCommandListener(new PrintWriter(Systemout)));
}
/
连接到FTP服务器
@param hostname 主机名
@param port 端口
@param username 用户名
@param password 密码
@return 是否连接成功
@throws IOException
/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClientconnect(hostname, port);
ftpClientsetControlEncoding("GBK");
if(FTPReplyisPositiveCompletion(ftpClientgetReplyCode())){
if(ftpClientlogin(username, password)){
return true;
}
}
disconnect();
return false;
}
/
从FTP服务器上下载文件,支持断点续传,上传百分比汇报
@param remote 远程文件路径
@param local 本地文件路径
@return 上传的状态
@throws IOException
/
public DownloadStatus download(String remote,String local) throws IOException{
//设置被动模式
ftpCliententerLocalPassiveMode();
//设置以二进制方式传输
ftpClientsetFileType(FTPBINARY_FILE_TYPE);
DownloadStatus result;
//检查远程文件是否存在
FTPFile[] files = ftpClientlistFiles(new String(remotegetBytes("GBK"),"iso-8859-1"));
if(fileslength != 1){
Systemoutprintln("远程文件不存在");
return DownloadStatusRemote_File_Noexist;
}
long lRemoteSize = files[0]getSize();
File f = new File(local);
//本地存在文件,进行断点下载
if(fexists()){
long localSize = flength();
//判断本地文件大小是否大于远程文件大小
if(localSize >= lRemoteSize){
Systemoutprintln("本地文件大于远程文件,下载中止");
return DownloadStatusLocal_Bigger_Remote;
}
//进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f,true);
ftpClientsetRestartOffset(localSize);
InputStream in = ftpClientretrieveFileStream(new String(remotegetBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=localSize /step;
int c;
while((c = inread(bytes))!= -1){
outwrite(bytes,0,c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
Systemoutprintln("下载进度:"+process);
//TODO 更新文件下载进度,值存放在process变量中
}
}
inclose();
outclose();
boolean isDo = ftpClientcompletePendingCommand();
if(isDo){
result = DownloadStatusDownload_From_Break_Success;
}else {
result = DownloadStatusDownload_From_Break_Failed;
}
}else {
OutputStream out = new FileOutputStream(f);
InputStream in= ftpClientretrieveFileStream(new String(remotegetBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
int c;
while((c = inread(bytes))!= -1){
outwrite(bytes, 0, c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
Systemoutprintln("下载进度:"+process);
//TODO 更新文件下载进度,值存放在process变量中
}
}
inclose();
outclose();
boolean upNewStatus = ftpClientcompletePendingCommand();
if(upNewStatus){
result = DownloadStatusDownload_New_Success;
}else {
result = DownloadStatusDownload_New_Failed;
}
}
return result;
}
/
上传文件到FTP服务器,支持断点续传
@param local 本地文件名称,绝对路径
@param remote 远程文件路径,使用/home/directory1/subdirectory/fileext 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构
@return 上传结果
@throws IOException
/
public UploadStatus upload(String local,String remote) throws IOException{
//设置PassiveMode传输
ftpCliententerLocalPassiveMode();
//设置以二进制流的方式传输
ftpClientsetFileType(FTPBINARY_FILE_TYPE);
ftpClientsetControlEncoding("GBK");
UploadStatus result;
//对远程目录的处理
String remoteFileName = remote;
if(remotecontains("/")){
remoteFileName = remotesubstring(remotelastIndexOf("/")+1);
//创建服务器远程目录结构,创建失败直接返回
if(CreateDirecroty(remote, ftpClient)==UploadStatusCreate_Directory_Fail){
return UploadStatusCreate_Directory_Fail;
}
}
//检查远程是否存在文件
FTPFile[] files = ftpClientlistFiles(new String(remoteFileNamegetBytes("GBK"),"iso-8859-1"));
if(fileslength == 1){
long remoteSize = files[0]getSize();
File f = new File(local);
long localSize = flength();
if(remoteSize==localSize){
return UploadStatusFile_Exits;
}else if(remoteSize > localSize){
return UploadStatusRemote_Bigger_Local;
}
//尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName, f, ftpClient, remoteSize);
//如果断点续传没有成功,则删除服务器上文件,重新上传
if(result == UploadStatusUpload_From_Break_Failed){
if(!ftpClientdeleteFile(remoteFileName)){
return UploadStatusDelete_Remote_Faild;
}
result = uploadFile(remoteFileName, f, ftpClient, 0);
}
}else {
result = uploadFile(remoteFileName, new File(local), ftpClient, 0);
}
return result;
}
/
断开与远程服务器的连接
@throws IOException
/
public void disconnect() throws IOException{
if(ftpClientisConnected()){
ftpClientdisconnect();
}
}
/
递归创建远程服务器目录
@param remote 远程服务器文件绝对路径
@param ftpClient FTPClient对象
@return 目录创建是否成功
@throws IOException
/
public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{
UploadStatus status = UploadStatusCreate_Directory_Success;
String directory = remotesubstring(0,remotelastIndexOf("/")+1);
if(!directoryequalsIgnoreCase("/")&&!ftpClientchangeWorkingDirectory(new String(directorygetBytes("GBK"),"iso-8859-1"))){
//如果远程目录不存在,则递归创建远程服务器目录
int start=0;
int end = 0;
if(directorystartsWith("/")){
start = 1;
}else{
start = 0;
}
end = directoryindexOf("/",start);
while(true){
String subDirectory = new String(remotesubstring(start,end)getBytes("GBK"),"iso-8859-1");
if(!ftpClientchangeWorkingDirectory(subDirectory)){
if(ftpClientmakeDirectory(subDirectory)){
ftpClientchangeWorkingDirectory(subDirectory);
}else {
Systemoutprintln("创建目录失败");
return UploadStatusCreate_Directory_Fail;
}
}
start = end + 1;
end = directoryindexOf("/",start);
//检查所有目录是否创建完毕
if(end <= start){
break;
}
}
}
return status;
}
/
上传文件到服务器,新上传和断点续传
@param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
@param localFile 本地文件File句柄,绝对路径
@param processStep 需要显示的处理进度步进值
@param ftpClient FTPClient引用
@return
@throws IOException
/
public UploadStatus uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{
UploadStatus status;
//显示进度的上传
long step = localFilelength() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile,"r");
OutputStream out = ftpClientappendFileStream(new String(remoteFilegetBytes("GBK"),"iso-8859-1"));
//断点续传
if(remoteSize>0){
ftpClientsetRestartOffset(remoteSize);
process = remoteSize /step;
rafseek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while((c = rafread(bytes))!= -1){
outwrite(bytes,0,c);
localreadbytes+=c;
if(localreadbytes / step != process){
process = localreadbytes / step;
Systemoutprintln("上传进度:" + process);
//TODO 汇报上传状态
}
}
outflush();
rafclose();
outclose();
boolean result =ftpClientcompletePendingCommand();
if(remoteSize > 0){
status = resultUploadStatusUpload_From_Break_Success:UploadStatusUpload_From_Break_Failed;
}else {
status = resultUploadStatusUpload_New_File_Success:UploadStatusUpload_New_File_Failed;
}
return status;
}
public static void main(String[] args) {
ContinueFTP myFtp = new ContinueFTP();
try {
Systemerrprintln(myFtpconnect("10106236", 21, "5", "jieyan"));
// myFtpftpClientmakeDirectory(new String("歌曲"getBytes("GBK"),"iso-8859-1"));
// myFtpftpClientchangeWorkingDirectory(new String("歌曲"getBytes("GBK"),"iso-8859-1"));
// myFtpftpClientmakeDirectory(new String("爱你等于爱自己"getBytes("GBK"),"iso-8859-1"));
// Systemoutprintln(myFtpupload("E:\\ywflv", "/ywflv",5));
// Systemoutprintln(myFtpupload("E:\\爱你等于爱自己mp4","/爱你等于爱自己mp4"));
//Systemoutprintln(myFtpdownload("/爱你等于爱自己mp4", "E:\\爱你等于爱自己mp4"));
myFtpdisconnect();
} catch (IOException e) {
Systemoutprintln("连接FTP出错:"+egetMessage());
}
}
}
以上就是关于java d出选择目录框(选择文件夹),获取选择的文件夹路径全部的内容,包括:java d出选择目录框(选择文件夹),获取选择的文件夹路径、java如何获取用户本地路径、java获取指定资源文件路径的几种方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)