下載文件的java代碼 下載文件的java代碼是什么
如何將下載的java源代碼導(dǎo)入到eclipse中運(yùn)行
eclipse打開并運(yùn)行一個已經(jīng)寫好的java文件步驟如下:
1、新建一個java工程項(xiàng)目:右鍵Eclipse的PackageExplorer空白部分,點(diǎn)擊New,再點(diǎn)擊JavaProject,輸入工程名,點(diǎn)擊finish;
2、在新建的工程里新建一個類:右鍵工程,點(diǎn)擊New,再點(diǎn)擊Class,輸入類名,點(diǎn)擊finish;
3、把寫好的java文件的代碼復(fù)制到新建的類中;
4、右鍵新建的類文件,點(diǎn)擊RunAs,再點(diǎn)擊JavaApplication即可運(yùn)行Java文件。
需要注意的是:java文件要成功運(yùn)行,前提是要有主方法(main)的存在,沒有主方法沒辦法運(yùn)行java程序。
Java文件下載怎么實(shí)現(xiàn)的
下載就很簡單了
把你要下載的文件做成超級鏈接,可以不用任何組件
比如說
下載一個word文檔
a href="名稱.doc"名稱.doc/a
路徑你自己寫
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java點(diǎn)虐.HttpURLConnection;
import java點(diǎn)虐.ProtocolException;
import java點(diǎn)虐.URI;
import java點(diǎn)虐.URL;
import java.util.Random;
/**
*
* 實(shí)現(xiàn)了下載的功能*/
public class SimpleTh {
public static void main(String[] args){
// TODO Auto-generated method stub
//String path = "倩女幽魂.mp3";//MP3下載的地址
String path ="";
try {
new SimpleTh().download(path, 3); //對象調(diào)用下載的方法
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getFilename(String path){//獲得文件的名字
return path.substring(path.lastIndexOf('/')+1);
}
public void download(String path,int threadsize) throws Exception//下載的方法
{//參數(shù) 下載地址,線程數(shù)量
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();//獲取HttpURLConnection對象
conn.setRequestMethod("GET");//設(shè)置請求格式,這里是GET格式
conn.setReadTimeout(5*1000);//
int filelength = conn.getContentLength();//獲取要下載文件的長度
String filename = getFilename(path);
File saveFile = new File(filename);
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.setLength(filelength);
accessFile.close();
int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1;
for(int threadid = 0;threadid=threadsize;threadid++){
new DownloadThread(url,saveFile,block,threadid).start();
}
}
private final class DownloadThread extends Thread{
private URL url;
private File saveFile;
private int block;//每條線程下載的長度
private int threadid;//線程id
public DownloadThread(URL url,File saveFile,int block,int threadid){
this.url = url;
this.saveFile= saveFile;
this.block = block;
this.threadid = threadid;
}
@Override
public void run() {
//計(jì)算開始位置的公式:線程id*每條線程下載的數(shù)據(jù)長度=?
//計(jì)算結(jié)束位置的公式:(線程id+1)*每條線程下載數(shù)據(jù)長度-1=?
int startposition = threadid*block;
int endposition = (threadid+1)*block-1;
try {
try {
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.seek(startposition);//設(shè)置從什么位置寫入數(shù)據(jù)
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5*1000);
conn.setRequestProperty("Range","bytes= "+startposition+"-"+endposition);
InputStream inStream = conn.getInputStream();
byte[]buffer = new byte[1024];
int len = 0;
while((len = inStream.read(buffer))!=-1){
accessFile.write(buffer, 0, len);
}
inStream.close();
accessFile.close();
System.out.println("線程id:"+threadid+"下載完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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();
// 設(shè)置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(); // 文件的默認(rèn)保存名
// 讀到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
// 設(shè)置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循環(huán)取出流中的數(shù)據(jù)
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();
}
}
// 下載網(wǎng)絡(luò)文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver點(diǎn)抗/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("" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名應(yīng)該編碼成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 下載文件
package?needlnotify;
import?java.io.*;
import?java點(diǎn)虐.*;
public?class?HomeWorkMyself2?{
????public?static?void?main(String...?args)?throws?IOException?{
????????download(new?URL(args[0]),?new?FileOutputStream(args[1])?);
????}
????/**
?????*?JDK?7?風(fēng)格的下載代碼
[email protected]
?????*/
????static?private?void?download(URL?url,OutputStream?out)?throws?IOException?{
????????byte[]?buf?=?new?byte[8096];
????????int?r;
????????try(?InputStream??ins?=?url.openStream()?)?{
????????????while((?r=ins.read(buf))!=-1?)?
????????????????out.write(buf,0,r);
????????}
????}
}
上一篇:java實(shí)現(xiàn)算數(shù)異常代碼 java算數(shù)測試代碼
欄 目:Java編程
下一篇:沒有了
本文標(biāo)題:下載文件的java代碼 下載文件的java代碼是什么
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/17282.html
您可能感興趣的文章
- 04-06java24小時正則代碼的簡單介紹
- 04-05java安全退出代碼 java退出按鈕的代碼
- 04-05刪除線性表的代碼java 線性表刪除操作數(shù)據(jù)結(jié)構(gòu)的算法實(shí)現(xiàn)
- 04-05java拷貝音樂源代碼 怎么拷貝java源文件
- 04-04java分析源代碼變量 java程序的源代碼編寫有哪些要點(diǎn)
- 04-04java代碼工程 一段java代碼的詳細(xì)解說
- 04-04包含Java11查看源代碼的詞條
- 04-04控制臺輸入java代碼 java的控制臺輸入命令
- 04-04java代碼注釋的位置 java代碼注釋的位置是什么
- 04-03蝸牛爬井java代碼 蝸牛爬井的數(shù)學(xué)公式


閱讀排行
本欄相關(guān)
- 04-06下載文件的java代碼 下載文件的java代
- 04-06java實(shí)現(xiàn)算數(shù)異常代碼 java算數(shù)測試代
- 04-06java24小時正則代碼的簡單介紹
- 04-06java時間計(jì)算代碼 java計(jì)算時間間隔
- 04-05java代碼可靠性 java代碼性能測試
- 04-05java安全退出代碼 java退出按鈕的代碼
- 04-05刪除線性表的代碼java 線性表刪除操作
- 04-05java項(xiàng)目完整代碼 java項(xiàng)目 代碼結(jié)構(gòu)層
- 04-05Java七夕代碼照片墻 java編程代碼圖片
- 04-05java拷貝音樂源代碼 怎么拷貝java源文
隨機(jī)閱讀
- 01-10C#中通過LRU實(shí)現(xiàn)通用高效的超時連接探
- 08-05Dedecms5.7調(diào)用最熱搜索關(guān)鍵詞標(biāo)簽(新
- 01-10C++ COM編程之什么是接口?
- 01-11在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全
- 01-10C#ComboBox控件“設(shè)置 DataSource 屬性后無
- 01-10C語言數(shù)據(jù)結(jié)構(gòu)之棧簡單操作
- 01-11asp文件用什么軟件編輯
- 08-05織夢dedecms如何通過修改代碼來實(shí)現(xiàn)中
- 01-10VBS教程:方法-GetFileName 方法
- 01-10Android仿微信語音對講錄音功能