導航:首頁 > 編程語言 > java下載文件到本地

java下載文件到本地

發布時間:2022-06-28 21:39:09

javaWeb如何將文件下載到本地.要求不要有提示下載框的,直接點擊後,就下載到某個本地盤下。

點擊後轉向執行的方法:先獲取點擊的文件路徑,然後通過讀取文件的IO流對象,放到緩沖流裡面,然後向網路傳輸文件流。

② java下載bat文件在本地運行的辦法

=Runtime.getRuntime().exec("cmd/crun.bat");//要執行的文件的路徑為run.bat//得到輸入流InputStreaminputStream=process.getInputStream();=newInputStreamReader(inputStream);BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);//得到輸出流OutputStreamoutputStream=process.getOutputStream();=newOutputStreamWriter(outputStream);BufferedWriterbufferedWriter=newBufferedWriter(outputStreamWriter);bufferedWriter.write("dd
");bufferedWriter.flush();Stringtemp=null;while((temp=bufferedReader.readLine())!=null){System.out.println(temp);}}

③ java代碼實現從svn伺服器下載文件到本地

首先你要安裝svn客戶端,安裝完成以後你右鍵選擇svn中的import,輸入你伺服器端代碼的地址,下載路徑什麼的自己配置,其他不用管,點擊OK就可以了,不過你要有read許可權才行。

④ Java如何利用url下載MP3保存到本地

Java如何利用url下載MP3保存的方法:

1 /** ;

2 * TODO 下載文件到本地 ;

3 * @author nadim ;
4 * @date Sep 11, 2015 11:45:31 AM ;

5 * @param fileUrl 遠程地址 ;

6 * @param fileLocal 本地路徑 ;

7 * @throws Exception ;
8 */ ;

9 public void downloadFile(String fileUrl,String fileLocal) throws Exception {;

10 URL url = new URL(fileUrl);

11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();

12 urlCon.setConnectTimeout(6000);

13 urlCon.setReadTimeout(6000);

14 int code = urlCon.getResponseCode();

15 if (code != HttpURLConnection.HTTP_OK) {

16 throw new Exception("文件讀取失敗");

17 }

18 //讀文件流;

19 DataInputStream in = new DataInputStream(urlCon.getInputStream());

20 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));

21 byte[] buffer = new byte[2048];

22 int count = 0;

23 while ((count = in.read(buffer)) > 0) {;

24 out.write(buffer, 0, count);

25 }

26 out.close();

27 in.close();

28 }。

Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。

Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程 。

⑤ Java 下載文件的方法怎麼寫

參考下面
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下載的文件的路徑。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的後綴名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下載文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}

// 下載本地文件
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
String fileName = "Operator.doc".toString(); // 文件的默認保存名
// 讀到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
// 設置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循環取出流中的數據
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 下載網路文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

//支持在線打開文件的一種方式
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在線打開方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名應該編碼成UTF-8
} else { // 純下載方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}

⑥ java 將頁面內容寫入excel文件中並可以將其下載到本地任意位置

java本身要生成excel文件必然是在後台做的,通過poi庫生成excel文件並製作表格。
無法直接通過網頁保存生成excel。
至於下載到本地任意位置,也是後台生成了excel文件發送到前台(瀏覽器),由用戶選擇要存在哪兒,不能直接存儲(這是web沙箱限制,不允許網頁直接訪問本地硬碟,不然你想想,如果你打開一個網頁,網頁代碼可以任意訪問你的硬碟,你還敢開網頁嗎)。
要繞過沙箱限制必須裝插件,也就是,你必須開發一個com或plugin插件,可以訪問本地硬碟,但這需要用戶手工安裝(比如flash的插件,你之所以能用網頁看flash是因為裝了它的插件,但這是你手工裝的,它不能繞過你直接給你裝,它必須詢問你行不行,你要手工點了OK,才能裝)

⑦ JAVA下載tomcat目錄下的文件,/home/tomcat目錄下有個1.txt文件,如何把這個文件給下載到本地

如果下載的文件在項目工程外,可以建立一個下載的action,該action以輸入流的方式讀取文件,並以輸出流的方式寫到前台,前台頁面點擊下載時調用該action即可。

⑧ java實現從伺服器下載tif文件到本地

不要考慮文件格式,你把文件以流的方式讀入在下載到本地就可以了

⑨ java下載伺服器上的文件到客戶端

java編程方法下載伺服器上的文件到本地客服端,代碼如下:

importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;

publicclassDownLoad{
publicstaticvoiddownloadFile(URLtheURL,StringfilePath)throwsIOException{
FiledirFile=newFile(filePath);
if(!dirFile.exists()){
//文件路徑不存在時,自動創建目錄
dirFile.mkdir();
}
//從伺服器上獲取圖片並保存
URLConnectionconnection=theURL.openConnection();
InputStreamin=connection.getInputStream();
FileOutputStreamos=newFileOutputStream(filePath+"\123.png");
byte[]buffer=newbyte[4*1024];
intread;
while((read=in.read(buffer))>0){
os.write(buffer,0,read);
}
os.close();
in.close();
}
publicstaticvoidmain(String[]args){
//下面添加伺服器的IP地址和埠,以及要下載的文件路徑
StringurlPath="http://伺服器IP地址:埠/image/123.png";

//下面代碼是下載到本地的位置
StringfilePath="d:\excel";

URLurl=newURL(urlPath);

try{

downloadFile(url,filePath);

}catch(IOExceptione){

e.printStackTrace();

}

}

}

⑩ java 如何下載文件

httpURLConnection conn;
conn.getInputStream;
再將這個stream 寫到文件就可以了

閱讀全文

與java下載文件到本地相關的資料

熱點內容
不去互聯網程序員 瀏覽:550
電腦qq郵箱解壓的圖片保存在哪裡 瀏覽:544
嵌入命令行 瀏覽:91
檔案為什麼被加密 瀏覽:485
十天學會單片機13 瀏覽:875
榮耀怎麼設置讓app一直運行 瀏覽:993
共享文件夾能在哪裡找到 瀏覽:435
旅遊訂旅店用什麼app 瀏覽:239
一個女程序員的聲音 瀏覽:496
魔術app怎麼用 瀏覽:340
單片機有4個8位的io口 瀏覽:897
win10rar解壓縮軟體 瀏覽:169
plc教程pdf 瀏覽:668
pythonshell清屏命令 瀏覽:279
檢測到加密狗注冊伺服器失敗 瀏覽:205
解壓後手機如何安裝 瀏覽:519
極客學院app為什麼下架 瀏覽:14
圖片批量壓縮綠色版 瀏覽:656
東北程序員帥哥 瀏覽:709
加密封條風噪小 瀏覽:975