大家好!是这样,服务器上有几个文件,想让用户下载,操作就是,页面上显示文件名称,可点击,点击之后用户就可以下载了。
1.怎么在页面上找到文件路径并显示? 2.怎么实现下载?(struts1+hibernate)先谢过各位!
------解决方案--------------------
前几天我写的下载固定文件的办法,直接用Servlet,jsp页面上直接上超链接
public class LoadFile extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
OutputStream o=response.getOutputStream();
byte b[]=new byte[1024];
//the file to download.
File fileLoad=new File("D:\\download","MRVInst.exe");
//the dialogbox of download file.
response.setHeader("Content-disposition",
"attachment;filename="+"MRVInst.exe");
//set the MIME type.
response.setContentType("application/application/octet-stream bin");
//get the file length.
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//download the file.
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
doGet(request,response);
}
}
------解决方案--------------------
页面找路径也是从服务器初始化给它的,可以给它一棵路径树
------解决方案--------------------
页面上 就把路径带着。下载通过流读取url执行下载操作。
------解决方案--------------------
package com.tqedu.crm.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tqedu.crm.Constants;
import com.tqedu.crm.log.TqLog;
import com.tqedu.crm.util.Tools;
/*
* @author Flex
* 文件下载的公共类
* filepath 必传
* 要下载的文件相对WebRott的路径,如: upload/stu_bespoke/3d0185611bc54be4aa6df1c722b081723765.zip
* 此地址也即MultiUploadServlet上传后返回的filepath值
* **/
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private TqLog logger = new TqLog(DownloadServlet.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filepath = request.getParameter("filepath");
if(Tools.isEmpty(filepath)){
writer(response, "<script>alert('您要下载的文件不存在!');</script>");
return;
}
String rootPath = this.getServletConfig().getServletContext().getRealPath(Constants.FilePath.SEPARATOR);
File f1 = new File(rootPath + filepath);
if (!f1.exists()) {
logger.warn("要下载的文件不存在: " + f1.getAbsolutePath());
writer(response, "<script>alert('您要下载的文件不存在!');</script>");
return;
}
logger.info("下载文件: " + f1.getAbsolutePath());
try {
InputStream in = new FileInputStream(f1);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;fileName=\"" + URLEncoder.encode(filepath.substring(filepath.lastIndexOf("/") + 1), "UTF-8") + "\"");
OutputStream os = response.getOutputStream();
byte[] buf = new byte[1024 * 1024];
while(in.read(buf) > -1){
os.write(buf);
}
os.flush();
os.close();
} catch (Exception e) {
logger.error("文件下载出错!", e);
writer(response, "<script>alert('文件下载出错!');</script>");
}
}
private void writer(HttpServletResponse response, String msg) throws IOException{
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.print(msg);
pw.flush();
pw.close();
}
}
页面用A标签就可以了 XX.action&filepath=xxxxxxxx