JSP Chinese download

zhaozj2021-02-16  77

JSP in Fuser Download Take it for a while before it comes to the required documents: aattchment.java and Downattach.jspattachment.java

public static void downloadAttachment (String fileName, HttpServletResponse response) {//filename=Test.doc ..... // the hard disk is converted into an actual address String fileOnDisk =; InputStream input = null; OutputStream output = null; File f = null Try {Try {INPUT = New FileInputStream (FileOndisk); f = new file (fileondisk);} catch (ioException e) {system.out.println ("can not get attchment on disk");}

BYTE [] Buffer = getBytes (Input); input.close (); input = null; output = response.getOutputStream ();

Response.setContentType ("Application / Oct-stream"); ​​Response.setHeader ("Location", FileName); Response.SetHeader ("Content-Disposition", "Attachment; FileName = /" " FileName " / "); Response.setContentLength ((int) f.Length ()); Output.write (); output.flush (); output.close (); output = null;} catch (ooException e) {system.out.println (@ IOEXCEPTION E) {system.out.println "Error Download Attachment.");} Finally {if (INPUT! = NULL) {Try {INPUT.CLOSE ();} Catch (ooException ex) {}}}}}}}}}}}}}}}}}}}}} (output! = null) {TRY {Output.close );} catch (IOException ex) {}}}} public static byte [] getBytes (inputStream inputStream) throws IOException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (1024); byte [] block = new byte [512]; while (true) {int readLength = inputStream.read (block); if (readLength == -1) break; byteArrayOutputStream.write (block, 0, readLength);} byte [] retValue = byteArrayOutputStream.toByte Array (); ByteArrayoutputStream.close (); Return RetValue;} DOWNATTA.JSP:

<% @ page language = "java" pageEncoding = "GB2312"%> <% @ page import = "Your.Package"%> <% ");" xxxxxxx ");

/ / Do not use setContentType (), each browser will automatically open the file, a bunch of garbled, argument value. String filename = Request.getParameter ("filename");

IF (filename == null || filename.Length () == 0) {return;}

Attachment.download (filename, response);%>

转载请注明原文地址:https://www.9cbs.com/read-12442.html

New Post(0)