本文目錄一覽:
- 1、下載的java源代碼怎麼打開
- 2、java下載服務器上的文件到客戶端
- 3、幫我看看這段java下載代碼,文件名為中文時,報錯,無法找到指定文件
- 4、Java中文件下載該怎麼寫代碼求高手指導
- 5、我有一段java http下載的代碼,但是我很多地方讀不懂,幫我謝謝注釋
下載的java源代碼怎麼打開
.class文件是java編譯後的文件,它不是源代碼,真正的java源代碼是.java文件。
java源代碼是txt格式的.java文件,用記事本就可以打開。
用eclipse打開java文件的方式是:
如果java文件是一個eclipse工程(根目錄帶有.project文件),用file/import/general/exist java project/(大概是)然後找到你的目錄。
否則需要自己新建一個工程file/new/java project
然後把java文件拷貝到.src目錄下。
.class文件是直接的編譯好的文件,可以用jad把.class文件反編譯成java文件,不過反編譯的代碼和原來的代碼不一定完全一樣。
java下載服務器上的文件到客戶端
java編程方法下載服務器上的文件到本地客服端,代碼如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownLoad {
public static void downloadFile(URL theURL, String filePath) throws IOException {
File dirFile = new File(filePath);
if(!dirFile.exists()){
//文件路徑不存在時,自動創建目錄
dirFile.mkdir();
}
//從服務器上獲取圖片並保存
URLConnection connection = theURL.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream os = new FileOutputStream(filePath+”\\123.png”);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = in.read(buffer)) 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
}
public static void main(String[] args) {
//下面添加服務器的IP地址和端口,以及要下載的文件路徑
String urlPath = “http://服務器IP地址:端口/image/123.png”;
//下面代碼是下載到本地的位置
String filePath = “d:\\excel”;
URL url = new URL(urlPath);
try {
downloadFile(url,filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
幫我看看這段java下載代碼,文件名為中文時,報錯,無法找到指定文件
代碼一共6句,前4句沒問題,運行下載是ok的,
最後一句有點不明白,為什麼用BufferedInputStream?這個是讀文件的,不知道你後面是如何寫的,難道還要把文件讀到內存?在向客戶端發送出去??這樣的話下載GB級文件內存溢出。
BufferedInputStream和BufferedOutputStream 用了之後,一定要flush(),這樣也許會解決你的中文下載報錯。
我直接用BufferedOutputStream 下載文件成功,前面是用你的,後面如下:
InputStream in = new FileInputStream(file);// 將文件裝換成緩衝流
OutputStream out = response.getOutputStream(); // 獲取response中得下載對象
BufferedOutputStream bufo = new BufferedOutputStream(out); // 對象轉換成字符流
int length = 0; // 讀取本地文時,記錄本次文件讀取內容大小
byte[] buffer = new byte[524288]; // 每次推送 512KB
while ((length = in.read(buffer)) != -1) // 讀取本地文件,並在存放在buffer 數組
{
bufo.write(buffer, 0, length);// 預備向客戶端推送
bufo.flush();// 清空緩存,並立即推送
}
in.close();
out.close();
bufo.close();
不建議寫 response.setContentLength(int); 大並發時,這個容易出問題。
Java中文件下載該怎麼寫代碼求高手指導
你的上傳做了嗎?
if (upfile.exists()) {
bytes = FileUtils.readFileToByteArray(upfile);
response.setContentType(“application/x-download”);
String agent = request.getHeader(“USER-AGENT”);//用戶代理
// 防止中文文件名亂碼
if (null != agent -1 != agent.indexOf(“MSIE”)) {
String codedfilename = StringUtils.replace(URLEncoder.encode(fileName, “UTF-8”), “+”, “%20”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + codedfilename);
} else if (null != agent -1 != agent.indexOf(“Mozilla”)) {
String codedfilename = MimeUtility.encodeText(fileName, “UTF-8”, “B”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + codedfilename);
} else {
response.setHeader(“Content-Disposition”, “attachment;filename=” + fileName);
}
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
}
我有一段java http下載的代碼,但是我很多地方讀不懂,幫我謝謝注釋
package API;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import Get.Other_API.*;
public class DownFile implements Runnable
{
private String LOCAL_PATH=”d:/”;
private String Down_Path=null;
//下面兩個是Down_Path的get和set的方法
public String getDown_Path() { return Down_Path;}
public void setDown_Path(String downPath) {Down_Path=downPath;}
public String getPath() { return LOCAL_PATH; }
public void setPath(String Path) {LOCAL_PATH=Path;}
public void DownNow()
{
//待下載文件地址
String fileUrl=getDown_Path();
InputStream in=null;
OutputStream out=null;
HttpURLConnection conn=null;
String fileName=null;
int count=0;
int finished=0;
int _temp=0;
try
{
//初始化連接
URL url=new URL(fileUrl);//將String類型的地址變為url對象
conn = (HttpURLConnection) url.openConnection();//開啟連接
conn.setDoInput(true);//必要的,開啟輸入輸出的設置
conn.setDoOutput(true);
//獲取文件名
String disposition=conn.getHeaderField(“Content-Disposition”);
if(disposition!=null!””.equals(disposition))
{
//從頭中獲取文件名
fileName=disposition.split(“;”)[1].split(“=”)[1].replaceAll(“\””,””);
}
else
{
//從地址中獲取文件名
fileName=fileUrl.substring(fileUrl.lastIndexOf(“/”)+1);
}
if(fileName!=null!””.equals(fileName))
{
//文件名解碼
fileName=URLDecoder.decode(fileName, “utf-8”);
}
else
{
System.out.println(“Error”);
//如果無法獲取文件名,則隨機生成一個
//fileName=”file_”+(int)(Math.random()*10);
}
//讀取數據
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)
{
//這種方法比較常用,得記住
byte[] buffer=new byte[2048];
in = conn.getInputStream();//獲取文本的輸入流
out=new FileOutputStream(new File(LOCAL_PATH,fileName));//確定輸出的地方
int size=conn.getContentLength();
while((count=in.read(buffer))!=-1)//不斷循環,每次讀取2048比特的數據
{
if(count!=0)
{
out.write(buffer,0,count);//將count大小的數據寫進去
finished+=count;//結尾的寫入的位置改變,為下次寫入做準備
if(_temp%500==0)
{
System.out.printf(“下載已完成—-%1$.2f%%\n”,(double)finished/size*100);//動態輸出下載的進度
_temp=0;
}
_temp++;
}
else
{
break;
}
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();//關閉輸出流
in.close();//關閉輸入流
conn.disconnect();//關閉連接,有打開就有關閉
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
//因為該類實現了Runnable接口,所以得實現這個run方法
@Override
public void run() {
// TODO Auto-generated method stub
DownNow();//調用上面的DownNow方法
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/246451.html