⑴ 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中的部分內容,就是如下的例子
首先將其分成兩個 按照// split("//")[1].split("\\.")[0] +". " + split("//")[1].split("\\.")[1]
⑶ 求用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如何獲取下載文件的文件名
File類裡面的getName()方法
⑸ java 獲得http下載文件的真實名稱
有兩種獲取鏈接文件名的方法:
第一種:從連接URL,從描述中獲取
比如這種地址:
http://book.booktxt.com/txtbuk/20130421/xuanhuan/2013043601180.rar
⑹ 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 把一個URL中的數據保存成本地文件,文件名為本機當前的系統時間
publicstaticStringdownloadLog(StringloadUrl,StringfileName)throwsException{
URLurl=newURL(loadUrl);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(50*1000);
conn.setRequestProperty("User-Agent",
"Mozilla/4.0(compatible;MSIE5.0;WindowsNT;DigExt)");
InputStreaminputStream=null;
FileOutputStreamfos=null;
inputStream=conn.getInputStream();
//路徑目錄
FilesaveDir=newFile("D://test");
if(!saveDir.exists()){
saveDir.mkdirs();
}
Filefile=newFile(saveDir+File.separator+fileName);
fos=newFileOutputStream(file);
readInputStream(fos,inputStream);
returnfile.toString();
}
/**
*用流把數據寫到本地文件上
*
*@paraminputStream
*@return
*@throwsException
*@throwsIOException
*/
(FileOutputStreamfos,
InputStreaminputStream)throwsException{
byte[]buffer=newbyte[1024];
intlen=0;
try{
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.flush();
}catch(Exceptione){
logger.error("readInputStream文件可能太大導致");
thrownewException(e);
}finally{
try{
fos.close();
inputStream.close();
}catch(IOExceptione){
}
}
}
⑼ Java 怎麼通過URL引用得到真正的文件名
//如果得到項目中的文件路徑統一資源定位符通過文件名獲取文件的絕對路徑
URLurl=Prop2.class.getResource("/a.properties");//importjava.net.URL;
System.out.println(url.getPath());
⑽ 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);