在jsp/servlet中断点/多线程下载文件
<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.io.RandomAccessFile" %>
<%!
public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r")
java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD())
response.setHeader("Server", "www.trydone.com")
response.setHeader("Accept-Ranges", "bytes")
long pos = 0
long len
len = raf.length()
if (request.getHeader("Range") != null) {
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT)
pos = Long.parseLong(request.getHeader("Range")
.replaceAll("bytes=", "")
.replaceAll("-", "")
)
}
response.setHeader("Content-Length", Long.toString(len - pos))
if (pos != 0) {
response.setHeader("Content-Range", new StringBuffer()
.append("bytes ")
.append(pos)
.append("-")
.append(Long.toString(len - 1))
.append("/")
.append(len)
.toString()
)
}
response.setContentType("application/octet-stream")
response.setHeader("Content-Disposition", new StringBuffer()
.append("attachmentfilename=\"")
.append(file.getName())
.append("\"").toString())
raf.seek(pos)
byte[] b = new byte[2048]
int i
OutputStream outs = response.getOutputStream()
while ((i = raf.read(b)) != -1) {
outs.write(b, 0, i)
}
raf.close()
fis.close()
}
%>
<%
String filePath = request.getParameter("file")
filePath = application.getRealPath(filePath)
File file = new File(filePath)
downloadFile(request, response, file)
%>
js 做不到 copy 到客户端指定位置
如果说的是java的话, 可以做到
import java.io.FileNotFoundExceptionimport java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.net.MalformedURLException
import java.net.URL
import java.net.URLConnection
/*
* 文 件 名: Test.java
* 版 权: XX Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
* 描 述: <描述>
* 修改时间: 2015-7-10
* 跟踪单号: <跟踪单号>
* 修改单号: <修改单号>
* 修改内容: <修改内容>
*/
/**
*
* @version [版本号, 2015-7-10]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class Test
{
public static void main(String[] args)
{
try
{
URLConnection openConnection = new URL("服务器文件的访问地址").openConnection()
InputStream is = openConnection.getInputStream()
byte[] buff = new byte[1024]
int len
FileOutputStream fos = new FileOutputStream("c:\\你的文件名.扩展名")
if (null != is)
{
while ((len = is.read(buff)) != -1)
{
fos.write(buff, 0, len)
}
}
fos.close()
is.close()
}
catch (MalformedURLException e)
{
e.printStackTrace()
}
catch (FileNotFoundException e)
{
e.printStackTrace()
}
catch (IOException e)
{
e.printStackTrace()
}
}
}
路径就是如:“/user/etc”。
解释:服务器的路径展现形式不是以盘符开始的,而是以“/”开始,之后的路径和windows系统无任何区别,如上面举例的路径,如果想从etc下拿文件,直接“cd /user/etc”之后找到想要的文件,进行下载即可。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)