WEBLOGIC file download does not eject Windows _weblogic

Source: Internet
Author: User

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 ;
  }
 }
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.