怎样通过java实现服务器上文件下载?
在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);
%>
package comletvdircloudutil;import comletvdircloudcontrollerDirectorWatermarkController;import orgslf4jLogger;import orgslf4jLoggerFactory;import javaio;import javanetHttpURLConnection;import javanetMalformedURLException;import javanetURL;/ Created by xijunge on 2016/11/24 0024 /public class HttpRequesterFile { private static final Logger log = LoggerFactorygetLogger(HttpRequesterFileclass); private static final String TAG = "uploadFile"; private static final int TIME_OUT = 100 1000; // 超时时间 private static final String CHARSET = "utf-8"; // 设置编码 / 上传文件到服务器 @param file 需要上传的文件 @param RequestURL 文件服务器的rul @return 返回响应的内容 / public static String uploadFile(File file, String RequestURL) throws IOException {
String result = null;
String BOUNDARY = "letv"; // 边界标识 随机生成 String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 内容类型 try {
URL url = new URL(RequestURL);
HttpURLConnection conn = (HttpURLConnection) urlopenConnection();
connsetReadTimeout(TIME_OUT);
connsetConnectTimeout(TIME_OUT);
connsetDoInput(true); // 允许输入流 connsetDoOutput(true); // 允许输出流 connsetUseCaches(false); // 不允许使用缓存 connsetRequestMethod("POST"); // 请求方式 connsetRequestProperty("Charset", CHARSET); // 设置编码 connsetRequestProperty("connection", "keep-alive");
connsetRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
你有FTPClient就比较好办,假如你的两台FTP服务器分别为fs1和fs2
在本地开发代码思路如下:
通过FTPClient连接上fs1,然后下载(可以循环批量下载)到本地服务器,保存到一个临时目录。
下载完成后,FTPClient断开与fs1的连接,记得必须logout。
本地服务器通过FileInputStream将刚下载到临时目录的文件读进来,得到一个List<File>集合。
通过FTPClient连接上fs2,循环List<File>集合,将文件上传至fs2的特定目录,然后清空临时目录,上传完毕后,断开fs2的连接,同样必须logout。
0条评论