導航:首頁 > 編程語言 > java從url獲取文件名

java從url獲取文件名

發布時間:2022-11-12 23:29:39

java 如何通過url獲取文件,url中含參數

通過HttpResponse獲得流,然後保存。

Ⅱ java代碼實現從路徑字元串中獲取文件名稱

這道題主要就是利用了String類的split()方法,進行多次調用,已經幫你實現了,代碼如下:

public class Test{
public static void main(String[] args){
String str = "c:/win/good/aaa.txt;d:/win/good/bbb.txt;c:/win/cccc.txt;";
//得到路徑數組
String[] fileRoot = str.split(";");
String[] fileName = null;
for(int i = 0;i < fileRoot.length;i++){
if(fileRoot[i] != null){
fileName = fileRoot[i].split("/");
//得到最終需要的文件名
System.out.println (fileName[fileName.length-1]);
}
}

}
}

Ⅲ 請問用java如何獲取下載文件的文件名

File類裡面的getName()方法

Ⅳ 求用java得到URL相應源文件的方法

Java可以通過鏈接的mime類型來判斷源文件的類型,從而得到源文件內容,示例如下:

URLConnection提供了兩種方法可以猜測(根據實測結果,這個猜測是相當的准)數據的MIME類型。
第一個是:
(Stringname)

這個方法根據URL文件部分的後綴名來判斷類型,跟之前我的方法一樣。這個不能解決上面那個問題。
第二個是:(InputStreamin)
這個方法是根據流的前面幾個位元組來判斷類型,這個就不需要文件後綴名了,完全可以解決上面那個問題。

測試代碼如下:BufferedInputStreambis=null;=null;URLurl=null;url=newURL(strUrl);urlconnection=(HttpURLConnection)url.openConnection();urlconnection.connect();bis=newBufferedInputStream(urlconnection.getInputStream());System.out.println("filetype:"+HttpURLConnection.guessContentTypeFromStream(bis));

Ⅳ java截取路徑字元串--得文件名

//舉例:
StringfName="G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif";

//方法一:

FiletempFile=newFile(fName.trim());

StringfileName=tempFile.getName();

System.out.println("方法一:fileName="+fileName);

//方法二:

fName=fName.trim();

//fileName=fName.substring(fName.lastIndexOf("/")+1);
//或者
fileName=fName.substring(fName.lastIndexOf("\")+1);

System.out.println("方法二:fileName="+fileName);

//方法三:

fName=fName.trim();

Stringtemp[]=fName.split("\\");/**split裡面必須是正則表達式,"\"的作用是對字元串轉義*/

//temp[]=[G:,Java_Source,navigation_tigra_menu,demo1,img,lev1_arrow.gif]
System.out.println("temp[]="+Arrays.toString(temp));
fileName=temp[temp.length-1];

System.out.println("方法三:fileName="+fileName);

Ⅵ java 獲得http下載文件的真實名稱

有兩種獲取鏈接文件名的方法:

第一種:從連接URL,從描述中獲取

比如這種地址:

http://book.booktxt.com/txtbuk/20130421/xuanhuan/2013043601180.rar

Ⅶ 使用java通過url獲得目標主機的文件,代碼如下,但是無法獲得該url下的文件,求各位大牛指點

報的錯誤是怎樣的。

捕捉錯誤後,列印出來看錯誤信息 e.printStackTrace();

String filePath = "d:/ghost/";

目錄是否存在。

Ⅷ Java 怎麼通過URL引用得到真正的文件名

//如果得到項目中的文件路徑統一資源定位符通過文件名獲取文件的絕對路徑
URLurl=Prop2.class.getResource("/a.properties");//importjava.net.URL;
System.out.println(url.getPath());

Ⅸ java 怎麼通過url獲取遠程伺服器上某個文件夾下的所有文件名

/**
* 讀取某個文件夾下的所有文件
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {

File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());

} else if (file.isDirectory()) {
System.out.println("文件夾");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());

} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}

}

} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}

閱讀全文

與java從url獲取文件名相關的資料

熱點內容
zblogphp插件 瀏覽:649
好興動app怎麼掌上取現 瀏覽:133
python折舊率 瀏覽:538
漣源雲伺服器 瀏覽:992
linux中代表什麼 瀏覽:33
androidphone模塊 瀏覽:35
linux中斷表 瀏覽:177
文本寫的java如何編譯運行 瀏覽:524
單片機pwm控制舵機 瀏覽:318
壓縮機sd是什麼 瀏覽:772
php獲取數據轉json數據 瀏覽:525
程序員入職比期望薪資高 瀏覽:889
蘋果電腦怎麼在桌面建新文件夾 瀏覽:526
蘋果上不小心買錯了app怎麼撤銷 瀏覽:845
蘋果手機在設置怎麼卸載app 瀏覽:317
我的世界基岩版正版伺服器地址 瀏覽:562
vci命令 瀏覽:834
快剪輯app如何提取音樂 瀏覽:950
gcc編譯器適合單片機開發嗎 瀏覽:886
自做app如何聯網 瀏覽:303