There is such a file download connection as follows:
<a href= "filename. xls" >download</a>
Under Tomcat, click Download, you can eject the file download box, but under the WebLogic but directly to open the file.
The reason: WebLogic is based on the HTTP Header file type to determine whether to eject the download box, the default file type is Contenttype= "text/html", so it is automatically opened. Tomcat is the default file as a binary file, so the pop-up dialog box.
So how do I get weblogic to pop up the dialog box, examples are as follows:
Test1.jsp contents are as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<script type= "Text/javascript" >
function Returnto (URL) {
window.location= ' test2.jsp?filename= ' +encodeuri (URL);
}
</script>
Note: The encodeURI function can be a good solution to the utf-8 coding common in the odd number of Chinese last one for garbled problem.
<title></title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<a href= "Javascript:returnto (' filename. xls ')" >download</a>
</body>
One: Adopt the RequestDispatcher way to carry out
Test2.jsp contents are as follows:
<%@ page language= "java" pageencoding= "Utf-8"%>
<%
String filename = request.getparameter ("filename");
Response.setcontenttype ("Application/x-download");/set to download Application/x-download
filename = new String (filename.getbytes ("iso-8859-1"), "Utf-8"); Resolves a problem where the GET request pass parameter is garbled.
String display = Java.net.URLEncoder.encode (filename, "utf-8"); Solve the file download box Chinese file name is garbled problem.
Response.AddHeader ("Content-disposition", "attachment;filename=" + display);
Try
{
RequestDispatcher dis = application.getrequestdispatcher ("/" +filename);
if (dis!= null)
{
Dis.forward (Request,response);
}
Response.flushbuffer ();
}
catch (Exception e)
{
E.printstacktrace ();
}
Finally
{
}
%>
Second: The use of file flow output mode download
Test2.jsp contents are as follows:
<%@ page language= "java" pageencoding= "Utf-8"%>
<%
Out.clear ();
Response.reset ();
Response.setcontenttype ("Application/x-download");
String filename = request.getparameter ("filename");
Response.setcontenttype ("Application/x-download");
filename = new String (filename.getbytes ("iso-8859-1"), "Utf-8");
String display = Java.net.URLEncoder.encode (filename, "utf-8");
Response.AddHeader ("Content-disposition", "attachment;filename=")
+ display); //
String path = application.getrealpath (filename);//Get Physical path
Java.io.OutputStream OUTP = null;
Java.io.FileInputStream in = null;
try {
OUTP = Response.getoutputstream ();
in = new Java.io.FileInputStream (path);
Byte[] B = new byte[1024];
int i = 0;
while ((i = In.read (b)) > 0) {
Outp.write (b, 0, I);
}
} catch (Exception e) {
system.out.println ("error!");
e.printstacktrace ();
} finally {
if (in!= null) {
in.close ();
in = null ;
}
}
%>