js| Download | page
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "java.io.*"%>
<%!
public string toutf8string (string s) {///from online
StringBuffer sb = new StringBuffer ();
for (int i=0;i<s.length (); i++) {
char C = S.charat (i);
if (c >= 0 && C <= 255) {
Sb.append (c);
} else {
Byte[] B;
try {
b = character.tostring (c). GetBytes ("Utf-8");
catch (Exception ex) {
System.out.println (ex);
b = new Byte[0];
}
for (int j = 0; J < B.length; J + +) {
int k = B[j];
if (k < 0) k = 256;
Sb.append ("%" + integer.tohexstring (k).
toUpperCase ());
}
}
}
return sb.tostring ();
}
%>
<%
String filename= "Test.jpg";
String Dirname=application.getrealpath ("/web-inf/upload");
Java.io.File Ff=null;
String dd=dirname+system.getproperties (). GetProperty ("File.separator") +filename;
try{
Ff=new Java.io.File (DD);
}
catch (Exception e) {
E.printstacktrace ();
}
if (Ff!=null&&ff.exists () &&ff.isfile ())
{
Long filelength = Ff.length ();
InputStream instream=new FileInputStream (DD);
Format the output
Response.reset ();
Response.setcontenttype ("Application/x-msdownload");
Response.setcontentlength ((int) filelength);
Response.AddHeader ("Content-disposition", "Attachment filename=\" "+ toutf8string (filename) +" ");
Loop out the data in the stream
Byte[] B = new byte[100];
int Len;
while ((Len=instream.read (b)) >0)
Response.getoutputstream (). write (B,0,len);
Instream.close ();
}
%>