Implement Web file download with HttpServlet

Source: Internet
Author: User
Tags config implement return string tostring trim stringbuffer
servlet|web| Download

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import com. topwisdom.framework.util.*;

public class Webdownload extends HttpServlet {
Public Webdownload () {
}

Private ServletConfig config;

public void init (ServletConfig config) throws servletexception {
Super.init (config);
this.config = config;
}

public void DoPost (HttpServletRequest req,httpservletresponse res) throws Servletexception {
Doget (Req,res);
}
file://get the name of the attachment
public static string Getattachname (string file_name) {
if (file_name==null) return "";
file_name = File_name.trim ();
int iPos = 0;
IPos = file_name.lastindexof ("\ \");
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
IPos = File_name.lastindexof ("/");
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
IPos = File_name.lastindexof (file.separator);
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
return file_name;
}
File://UTF8 Transfer Code
public static string toutf8string (string s) {
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 ());
}
}
}
String S_utf8 = sb.tostring ();
Sb.delete (0,sb.length ());
Sb.setlength (0);
SB = null;
return S_utf8;
}
file://Obtain the true full path name of the downloaded file
Private String Getrealname (httpservletrequest request,string file_name) {
if (Request==null | | file_name==null) return NULL;
file_name = File_name.trim ();
if (File_name.equals ("")) return null;

String File_path = Request.getrealpath (file_name);
if (file_path== null) return null;
File File = new file (File_path);
if (!file.exists ()) return null;
return file_path;
}
file://implementation Download
public void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception {
String file_name = Request.getparameter ("file_name");
if (file_name==null) file_name = "";
file_name = File_name.trim ();

InputStream instream= null;
String attch_name = "";

Byte[] B = new byte[100];
int len= 0;
try {
file://get the name of the attachment
Attch_name = Getattachname (file_name);

file_name = Getrealname (request,file_name);
if (file_name==null) {
SYSTEM.OUT.PRINTLN ("File does not exist, or prohibit downloading");
return;
}
Attch_name = toutf8string (attch_name);
file://read into the stream
Instream=new FileInputStream (file_name);
file://format of output
Response.reset ();
Response.setcontenttype ("Application/x-msdownload");


Response.AddHeader ("Content-disposition", "attachment; filename=\" "+ attch_name +" \ ");
file://loop to remove data from the stream
while ((Len=instream.read (b)) >0) {
Response.getoutputstream (). write (B,0,len);
}
Instream.close ();
}catch (Exception e) {
if (e instanceof java.io.FileNotFoundException) {
try {
Response.sendredirect ("/tip/file_not_found.html");
}
catch (IOException ex) {
Ex.printstacktrace (System.err);
}
}
else {
E.printstacktrace (System.err);
}
}
}
}



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.