怎么从服务器上下载文件的代码 ?
private void DownLoad(string path)
{
try
{
FileInfo DownloadFile = new FileInfo(path);
ResponseClear();
ResponseClearHeaders();
ResponseBuffer = false;
ResponseContentType = "application/octet-stream";
ResponseAppendHeader("Content-Disposition", "attachment;filename=" + HttpUtilityUrlEncode(DownloadFileFullName, SystemTextEncodingUTF8) + ";charset=GB2312");
ResponseAppendHeader("Content-Length", DownloadFileLengthToString());
ResponseWriteFile(DownloadFileFullName);
ResponseFlush();
}
catch
{
//ResponseWrite(GlobalClassScriptAlert("下载文件失败!"));
}
}
用copy命令,
将远程主机的文件复制到自己的电脑:copy
\\ip地址\c$\文件名\c:\
当然也可以把本地文件复制到远程主机:
copy
c:\文件名\\ip地址\c$
如果是ftp主机比如5944,可以在网页上直接登陆,打开ie输入
ftp://ftp分配给你的ip地址
回车后要在对话框里输入ftp分配的用户和密码
如果登陆成功,可以把浏览器的页面框缩小后以拖拽的方式把ftp上的文件拖到本地桌面。
具体在cmd命令下的ftp命令实在太多,这里就不用说了。
java编程方法下载服务器上的文件到本地客服端,代码如下:
import javaioBufferedWriter;import javaioFile;
import javaioFileOutputStream;
import javaioFileWriter;
import javaioIOException;
import javaioInputStream;
import javanetURL;
import javanetURLConnection;
public class DownLoad {
public static void downloadFile(URL theURL, String filePath) throws IOException {
File dirFile = new File(filePath);
if(!dirFileexists()){
//文件路径不存在时,自动创建目录
dirFilemkdir();
}
//从服务器上获取并保存
URLConnection connection = theURLopenConnection();
InputStream in = connectiongetInputStream();
FileOutputStream os = new FileOutputStream(filePath+"\\123png");
byte[] buffer = new byte[4 1024];
int read;
while ((read = inread(buffer)) > 0) {
oswrite(buffer, 0, read);
}
osclose();
inclose();
}
public static void main(String[] args) {
//下面添加服务器的IP地址和端口,以及要下载的文件路径
String urlPath = "http://服务器IP地址:端口/image/123png";
//下面代码是下载到本地的位置
String filePath = "d:\\excel";
URL url = new URL(urlPath);
try {
downloadFile(url,filePath);
} catch (IOException e) {
eprintStackTrace();
}
}
}
在jsp/servlet中断点/多线程下载文件
<%@ page import="javaioFile" %><%@ page import="javaioIOException" %>
<%@ page import="javaioOutputStream" %>
<%@ page import="javaioRandomAccessFile" %>
<%!
public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r");
javaioFileInputStream fis = new javaioFileInputStream(rafgetFD());
responsesetHeader("Server", "wwwtrydonecom");
responsesetHeader("Accept-Ranges", "bytes");
long pos = 0;
long len;
len = raflength();
if (requestgetHeader("Range") != null) {
responsesetStatus(HttpServletResponseSC_PARTIAL_CONTENT);
pos = LongparseLong(requestgetHeader("Range")
replaceAll("bytes=", "")
replaceAll("-", "")
);
}
responsesetHeader("Content-Length", LongtoString(len - pos));
if (pos != 0) {
responsesetHeader("Content-Range", new StringBuffer()
append("bytes ")
append(pos)
append("-")
append(LongtoString(len - 1))
append("/")
append(len)
toString()
);
}
responsesetContentType("application/octet-stream");
responsesetHeader("Content-Disposition", new StringBuffer()
append("attachment;filename=\"")
append(filegetName())
append("\"")toString());
rafseek(pos);
byte[] b = new byte[2048];
int i;
OutputStream outs = responsegetOutputStream();
while ((i = rafread(b)) != -1) {
outswrite(b, 0, i);
}
rafclose();
fisclose();
}
%>
<%
String filePath = requestgetParameter("file");
filePath = applicationgetRealPath(filePath);
File file = new File(filePath);
downloadFile(request, response, file);
%>
0条评论