问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

JAVA 文件下载问题

发布网友 发布时间:2022-04-29 17:15

我来回答

2个回答

热心网友 时间:2023-10-22 15:26

如果是本地服务器的话,应该把文件发布,会生成一个url ,用这个url就可以下载了。

热心网友 时间:2023-10-22 15:26

package Task;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpDownload {
private String file_url;
private String file_path;
private String file_name;
private String logo_url;
private String logo_path;
private String logo_name;

public void setLogo_name(String logoUrl) {
this.logo_name = logoUrl.substring(logoUrl.lastIndexOf("/"));
}
public void setLogo_path(String logoUrl) {
String Path = "d:\\logo";
String partPath = logo_name.substring(1,logo_name.lastIndexOf("."));
this.logo_path = Path ;
List<String> list = new ArrayList<String>();
int beginIndex = 1;
int endIndex = 4;
while(beginIndex<partPath.length()-1){
if(endIndex>partPath.length()-1) endIndex = partPath.length();
String name = partPath.substring(beginIndex, endIndex);
if(name.length()<3){
for(int i = name.length();i<3;i++){
name+=0;
}
}
list.add(name);
beginIndex += 3;
endIndex += 3;
}
for(String s:list){
logo_path += "\\"+s;
}
System.out.println(logo_path);
}
public void setFile_name(String file_name) {
this.file_name = file_name.substring(file_name.lastIndexOf("/"));
}
public boolean downLoadFile(String sURL,String name,String path) {
int nStartPos = 0;
int nRead = 0;
try {
URL url = new URL(sURL);
// 打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
// 获得文件长度
long nEndPos = getFileSize(sURL);
File file = new File(path);
file.mkdirs();
RandomAccessFile oSavedFile = new RandomAccessFile(path + "\\"
+ name, "rw");
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
// 告诉服务器,这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(name+"下载开始:"+sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
// 读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
System.out.println(name+"文件下载结束!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}

}

public String getFile_url() {
return file_url;
}
public void setFile_url(String file_url) {
this.file_url = file_url;
}
public String getFile_path() {
return file_path;
}
public void setFile_path() {
this.file_path = "D:\\soft";
}
public String getFile_name() {
return file_name;
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");

int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}

public String getLogo_url() {
return logo_url;
}
public void setLogo_url(String logo_url) {
this.logo_url = logo_url;
}
public String getLogo_name() {
return logo_name;
}
public String getLogo_path() {
return logo_path;
}
}
这是跑在我服务器上的下载程序你可以看看希望对你有帮助,有问题问就好了

热心网友 时间:2023-10-22 15:26

如果是本地服务器的话,应该把文件发布,会生成一个url ,用这个url就可以下载了。

热心网友 时间:2023-10-22 15:26

package Task;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpDownload {
private String file_url;
private String file_path;
private String file_name;
private String logo_url;
private String logo_path;
private String logo_name;

public void setLogo_name(String logoUrl) {
this.logo_name = logoUrl.substring(logoUrl.lastIndexOf("/"));
}
public void setLogo_path(String logoUrl) {
String Path = "d:\\logo";
String partPath = logo_name.substring(1,logo_name.lastIndexOf("."));
this.logo_path = Path ;
List<String> list = new ArrayList<String>();
int beginIndex = 1;
int endIndex = 4;
while(beginIndex<partPath.length()-1){
if(endIndex>partPath.length()-1) endIndex = partPath.length();
String name = partPath.substring(beginIndex, endIndex);
if(name.length()<3){
for(int i = name.length();i<3;i++){
name+=0;
}
}
list.add(name);
beginIndex += 3;
endIndex += 3;
}
for(String s:list){
logo_path += "\\"+s;
}
System.out.println(logo_path);
}
public void setFile_name(String file_name) {
this.file_name = file_name.substring(file_name.lastIndexOf("/"));
}
public boolean downLoadFile(String sURL,String name,String path) {
int nStartPos = 0;
int nRead = 0;
try {
URL url = new URL(sURL);
// 打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
// 获得文件长度
long nEndPos = getFileSize(sURL);
File file = new File(path);
file.mkdirs();
RandomAccessFile oSavedFile = new RandomAccessFile(path + "\\"
+ name, "rw");
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
// 告诉服务器,这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(name+"下载开始:"+sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
// 读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
System.out.println(name+"文件下载结束!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}

}

public String getFile_url() {
return file_url;
}
public void setFile_url(String file_url) {
this.file_url = file_url;
}
public String getFile_path() {
return file_path;
}
public void setFile_path() {
this.file_path = "D:\\soft";
}
public String getFile_name() {
return file_name;
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");

int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}

public String getLogo_url() {
return logo_url;
}
public void setLogo_url(String logo_url) {
this.logo_url = logo_url;
}
public String getLogo_name() {
return logo_name;
}
public String getLogo_path() {
return logo_path;
}
}
这是跑在我服务器上的下载程序你可以看看希望对你有帮助,有问题问就好了

热心网友 时间:2023-10-22 15:26

如果是本地服务器的话,应该把文件发布,会生成一个url ,用这个url就可以下载了。

热心网友 时间:2023-10-22 15:26

package Task;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpDownload {
private String file_url;
private String file_path;
private String file_name;
private String logo_url;
private String logo_path;
private String logo_name;

public void setLogo_name(String logoUrl) {
this.logo_name = logoUrl.substring(logoUrl.lastIndexOf("/"));
}
public void setLogo_path(String logoUrl) {
String Path = "d:\\logo";
String partPath = logo_name.substring(1,logo_name.lastIndexOf("."));
this.logo_path = Path ;
List<String> list = new ArrayList<String>();
int beginIndex = 1;
int endIndex = 4;
while(beginIndex<partPath.length()-1){
if(endIndex>partPath.length()-1) endIndex = partPath.length();
String name = partPath.substring(beginIndex, endIndex);
if(name.length()<3){
for(int i = name.length();i<3;i++){
name+=0;
}
}
list.add(name);
beginIndex += 3;
endIndex += 3;
}
for(String s:list){
logo_path += "\\"+s;
}
System.out.println(logo_path);
}
public void setFile_name(String file_name) {
this.file_name = file_name.substring(file_name.lastIndexOf("/"));
}
public boolean downLoadFile(String sURL,String name,String path) {
int nStartPos = 0;
int nRead = 0;
try {
URL url = new URL(sURL);
// 打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
// 获得文件长度
long nEndPos = getFileSize(sURL);
File file = new File(path);
file.mkdirs();
RandomAccessFile oSavedFile = new RandomAccessFile(path + "\\"
+ name, "rw");
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
// 告诉服务器,这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(name+"下载开始:"+sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
// 读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
System.out.println(name+"文件下载结束!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}

}

public String getFile_url() {
return file_url;
}
public void setFile_url(String file_url) {
this.file_url = file_url;
}
public String getFile_path() {
return file_path;
}
public void setFile_path() {
this.file_path = "D:\\soft";
}
public String getFile_name() {
return file_name;
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");

int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}

public String getLogo_url() {
return logo_url;
}
public void setLogo_url(String logo_url) {
this.logo_url = logo_url;
}
public String getLogo_name() {
return logo_name;
}
public String getLogo_path() {
return logo_path;
}
}
这是跑在我服务器上的下载程序你可以看看希望对你有帮助,有问题问就好了

热心网友 时间:2023-10-22 15:26

如果是本地服务器的话,应该把文件发布,会生成一个url ,用这个url就可以下载了。

热心网友 时间:2023-10-22 15:26

package Task;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpDownload {
private String file_url;
private String file_path;
private String file_name;
private String logo_url;
private String logo_path;
private String logo_name;

public void setLogo_name(String logoUrl) {
this.logo_name = logoUrl.substring(logoUrl.lastIndexOf("/"));
}
public void setLogo_path(String logoUrl) {
String Path = "d:\\logo";
String partPath = logo_name.substring(1,logo_name.lastIndexOf("."));
this.logo_path = Path ;
List<String> list = new ArrayList<String>();
int beginIndex = 1;
int endIndex = 4;
while(beginIndex<partPath.length()-1){
if(endIndex>partPath.length()-1) endIndex = partPath.length();
String name = partPath.substring(beginIndex, endIndex);
if(name.length()<3){
for(int i = name.length();i<3;i++){
name+=0;
}
}
list.add(name);
beginIndex += 3;
endIndex += 3;
}
for(String s:list){
logo_path += "\\"+s;
}
System.out.println(logo_path);
}
public void setFile_name(String file_name) {
this.file_name = file_name.substring(file_name.lastIndexOf("/"));
}
public boolean downLoadFile(String sURL,String name,String path) {
int nStartPos = 0;
int nRead = 0;
try {
URL url = new URL(sURL);
// 打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
// 获得文件长度
long nEndPos = getFileSize(sURL);
File file = new File(path);
file.mkdirs();
RandomAccessFile oSavedFile = new RandomAccessFile(path + "\\"
+ name, "rw");
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
// 告诉服务器,这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(name+"下载开始:"+sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
// 读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
System.out.println(name+"文件下载结束!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}

}

public String getFile_url() {
return file_url;
}
public void setFile_url(String file_url) {
this.file_url = file_url;
}
public String getFile_path() {
return file_path;
}
public void setFile_path() {
this.file_path = "D:\\soft";
}
public String getFile_name() {
return file_name;
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection
.setRequestProperty("User-Agent", "Internet Explorer");

int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}

public String getLogo_url() {
return logo_url;
}
public void setLogo_url(String logo_url) {
this.logo_url = logo_url;
}
public String getLogo_name() {
return logo_name;
}
public String getLogo_path() {
return logo_path;
}
}
这是跑在我服务器上的下载程序你可以看看希望对你有帮助,有问题问就好了
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
怀孕四个月补钙可以吗 孕妇吃什么时候钙片好 孕妇吃什么钙片最好 怀孕补钙是必须的吗?孕妇钙片品牌哪个好? ...在长高的黄金发育时期,会影响容貌五官的变化吗,不仅没有发育到变... 冬至吃饺子的诗词 立冬吃饺子的暖心诗词 宁波全封闭学校有哪些 宁波市北仑区小港镇会计学校在那? 或红联镇也可以 江南教育集团小港中心幼儿园园况介绍 红联哪里有报暑假班 五星牌壁挂太阳能热水器没有加介质口怎么办 破壁机霍尔异常什么意思 java IO流下个程序在网站下载东西 如何用java语言直接从web上下载数据,从而省去在网页上手动点击下载? 急!给出一个URI,对应的是服务端的的一个zip包,客户端如何用java对它进行下载, 计算机的注册表怎么打开? java中URL下载文件的问题。程序有问题,下载的mp3文件比原来的大,请问问题出在哪里? 太阳能热水器介质? 使用java编写一个多线程下载器,需要在URL栏中输入网址,然后通过网址下载。该怎么实现,求源代码 mongodb的作品目录 太阳能热水需要什么液体? 请问如何在计算机上打开注册表 从网上通过url获得图片能否先根据url压缩完在保存到本地java怎么实现啊,有没有会的啊,大神们,压缩后的图 中科蓝天太阳能带电两用热水器不用太阳能光用电不加介质能加热吗? java如何实现超链接下载 香酥的大油条如何才能做的更加美味呢? 不加介质能加热吗蓝天中科太阳能电两用热水器怎样加介质? 安卓如何实现输入url通过url将网络资源下载并储存到本地(无论什么文件类型都可以下载) 注册表怎么打开 windows 用java流的方式怎么指定下载到指定目录下 自行车锁死把套和普通把套的区别?求详细 如何用java实现下载文件(包括图片) 自行车把套,可锁死把套是什么意思? java如何将本地文件返回给对方一个下载链接(http方式下载) 山地车都用把套,但是把套又有什么用呢?它的作用很大吗? 破壁机可以空杯运转么? 山地车把套什么牌子好 苹果8p在辅助功能里面打不开声音,识别怎么回事?它老闪退出来 自行车XXF和PRO的锁死把套哪个好,50的那种 破壁机低速不转高速转怎么回事情? 自行车自行车不可锁死把套怎么取下 山地车把套单边锁死双边锁死哪个好 山地车燕尾把的什么把套最好 前面输入文字直接覆盖了后面的文字?怎么才不会覆盖啊?··· 手机护眼模式,真的有用吗 微信电脑版输入的时候,我要修改其他的字时,后面的字就会被覆盖,谢谢... 手机上的护眼模式有用吗 手机“护眼模式”有用吗 手机护眼模式真的有用吗垃圾屏幕辣眼睛 我想把word里面两页大的表格打印在一张A4纸张上面,需要怎么调?