jsp+servlet 客户端文件保存/下载问题!
一般情况下客户端文件下载大致形式是:
<a href="a.gif">图片</a>
<a href="b.wav">媒体文件</a>
<a href="c.mp3">声音文件</a>
这种连接有的单击后出现下载对话框,但有的则直接播放。
现在要求无论那种格式文件,都出现下载对话框,请问如何解决?如果多种文件格式不行,则现在至少要实现tif和wav格式文件。向各位讨教!!
我现在有个办法,但不知道是否可行:
单及连接后,将文件路径名称传人servlet(extend HttpServlet),在doGet方法中用流写出来,不知道如何实现!!
问题点数:100、回复次数:6Top
1 楼zxp_net(好心情)回复于 2003-09-03 22:49:11 得分 0
因为考虑可操作性,不能使用右键单击另存为的方式下载。最好通过servlet或action方式实现。Top
2 楼leshui(大象无形)(有物混成,先天地生)回复于 2003-09-04 11:43:13 得分 0
写一个下载的程序就可以了
http://www.csdn.net/develop/read_article.asp?id=19064Top
3 楼ticlej(ticlej)回复于 2003-09-04 12:22:41 得分 0
用servlet实现最好,除了对资源可控之外,也能更好处理客户信息Top
4 楼zxp_net(好心情)回复于 2003-09-04 15:13:32 得分 0
用servlet可以实现,但现在不能下载中文文件,当下载中文文件时,出现
java.io.FileNotFoundException: ..\file_share\%B9%D8%B1%D5%CA%B1%D7%E0%BB%C3%CF%EB%BF%............错误信息。请帮忙看看!!谢谢
得到文件路径和文件名称后,通过system.out.println在servlet中打印中文字符正常,
之后
response.setContentType("application/binary; charset=ISO8859_1");
String fileName = request.getAttribute("fileName").toString();
fileName = new String(fileName.getBytes("ISO8859_1"),"GB2312");
System.out.println("new file = " + fileName);//正常
fileName = URLEncoder.encode(fileName);
但
File file = new File("..//file_share//" + fileName);
new BufferedInputStream(new FileInputStream(file)); //但在这里就报上面的异常Top
5 楼swallowsky(子痕)回复于 2003-09-04 15:31:39 得分 0
File file = new File("..//file_share//" + fileName);
new BufferedInputStream(new FileInputStream(file)); //但在这里就报上面的异常
路径错了,斜杠应该反过来写。如果在windows系统中Top
6 楼shipp(嘎子)回复于 2003-09-15 15:39:43 得分 100
用servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.URLEncoder;
import java.net.MalformedURLException;
public class DownloadServlet extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//这里填入服务器的url
private String serverUrl = "";
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
/////////////////////////////////////////////
if(request.getAttribute("checkMimeType") == null || !"true".equals(request.getAttribute("checkMimeType").toString()))
{
java.util.Enumeration enum = request.getHeaderNames();
//如果不是从网站过以的连接,不允许下载
if(request.getHeader("referer") == null ||
( (String) request.getHeader("referer")).trim().indexOf(serverUrl) < 0)
{
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=GB2312\">");
out.println("</head>");
out.println("<body>");
out.println("您无权下载该文件!");
out.println("</body>");
out.println("</html>");
return;
}
}
String fileName = request.getAttribute("fileName").toString();
// fileName = URLEncoder.encode(fileName);
ServletOutputStream out = response.getOutputStream();
//如果需要根据文件指定MimeType
if(request.getAttribute("checkMimeType") != null && "true".equals(request.getAttribute("checkMimeType").toString()))
{
if(fileName.toLowerCase().indexOf(".jpg") > 0 || fileName.toLowerCase().indexOf(".jpeg") > 0)
{
response.setContentType("image/jpeg; charset=ISO8859_1");
}
else if(fileName.toLowerCase().indexOf(".gif") > 0)
{
response.setContentType("image/gif; charset=ISO8859_1");
}
else if(fileName.toLowerCase().indexOf(".bmp") > 0)
{
response.setContentType("image/bmp; charset=ISO8859_1");
}
else if(fileName.toLowerCase().indexOf(".ico") > 0)
{
response.setContentType("image/x-icon; charset=ISO8859_1");
}
else if(fileName.toLowerCase().indexOf(".png") > 0)
{
response.setContentType("image/png; charset=ISO8859_1");
}
else
{
response.setContentType("application/binary; charset=ISO8859_1");
}
}
else
{
response.setContentType("application/binary; charset=ISO8859_1");
}
fileName = new String(fileName.getBytes("GB2312"),"ISO8859_1"); //add by dl
System.out.println("IE " + fileName);
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String fileUrl = request.getAttribute("fileUrl").toString();
System.out.println("--------debug: " + fileUrl);
try
{
File file = new File(fileUrl);
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while( -1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
bos.flush();
bos.close();
bis.close();
out.close();
out = null;
bis = null;
bos = null;
buff = null;
}
catch(MalformedURLException e)
{
System.out.println("下载文件时发生了MalformedURLExceptin错误" + e.toString());
throw e;
}
catch(IOException e)
{
System.out.println("下载文件时发生了IOException错误" + e.toString());
throw e;
}
catch(Exception e)
{
System.out.println("下载文件时发生错误" + e.toString());
}
finally
{
if(bis != null)
{
bis.close();
}
if(bos != null)
{
bos.close();
}
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
//Clean up resources
public void destroy()
{
}
}
Top



