远程上载
远程下载
有针性的下载,实现strust文件下载,action控制如下:
jsp读取资源如下:
有针性的下载,实现strust文件下载,action控制如下:
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package org.topxp.struts; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; /** * MyEclipse Struts * Creation date: 10-01-2007 * * XDoclet definition: * @struts.action * @struts.action-forward name="index" path="/resources/index.jsp" */ public class DownloadServerAction extends DispatchAction { @Override protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //读取资源,并提供下载 DownloadServerActionForm df = (DownloadServerActionForm)form; File root = new File("/"); if(df.getName() != null && !df.getName().equals("")){ root = new File(df.getName()); } request.setAttribute("path", root.getPath()); //提供下载 if(root.isFile()){ java.io.File f = root; java.io.FileInputStream fis = new java.io.FileInputStream(f); response.reset(); response.setHeader("Server", "playyuer@Microshaoft.com"); //告诉客户端允许断点续传多线程连接下载 //响应的格式是: //Accept-Ranges: bytes response.setHeader("Accept-Ranges", "bytes"); long p = 0; long l = 0; //l = raf.length(); l = f.length(); //如果是第一次下,还没有断点续传,状态是默认的 200,无需显式设置 //响应的格式是: //HTTP/1.1 200 OK if (request.getHeader("Range") != null) //客户端请求的下载的文件块的开始字节 { //如果是下载文件的范围而不是全部,向客户端声明支持并开始文件块下载 //要设置状态 //响应的格式是: //HTTP/1.1 206 Partial Content response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);//206 //从请求中得到开始的字节 //请求的格式是: //Range: bytes=[文件块的开始字节]- p = Long.parseLong(request.getHeader("Range").replaceAll("bytes=","").replaceAll("-","")); } //下载的文件(或块)长度 //响应的格式是: //Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节] response.setHeader("Content-Length", new Long(l - p).toString()); if (p != 0) { //不是从最开始下载, //响应的格式是: //Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小] response.setHeader("Content-Range","bytes " + new Long(p).toString() + "-" + new Long(l -1).toString() + "/" + new Long(l).toString()); } //response.setHeader("Connection", "Close"); //如果有此句话不能用 IE 直接下载 //使客户端直接下载 //响应的格式是: //Content-Type: application/octet-stream response.setContentType("application/octet-stream"); //为客户端下载指定默认的下载文件名称 //响应的格式是: //Content-Disposition: attachment;filename="[文件名]" //response.setHeader("Content-Disposition", "attachment;filename=\"" + s.substring(s.lastIndexOf("\\") + 1) + "\""); //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句 String downloadname = URLEncoder.encode(f.getName()); response.setHeader("Content-Disposition", "attachment;filename=\"" + downloadname + "\""); //raf.seek(p); fis.skip(p); byte[] b = new byte[1024]; int i; //while ( (i = raf.read(b)) != -1 ) //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句 while ( (i = fis.read(b)) != -1 ) { response.getOutputStream().write(b,0,i); } //raf.close();//经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句 fis.close(); return null; }else{ File[] files = root.listFiles(); if(root.getParentFile() != null){ String parentPath = root.getParentFile().getPath(); request.setAttribute("parent", parentPath); }else{ request.setAttribute("parent", root.getPath()); } request.setAttribute("files", files); } return mapping.findForward("index"); } }
jsp读取资源如下:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${path } 【<a href="resources.do?name=${parent }">返回</a>】 <hr> <c:forEach items="${files}" var="file"> <a href="resources.do?name=${file.path }">${file.name }</a> <br> </c:forEach> </body> </html>