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;
}
}
这是跑在我服务器上的下载程序你可以看看希望对你有帮助,有问题问就好了