Home > Back-end >  Download springboot TXT file will return will return to the success of the background information
Download springboot TXT file will return will return to the success of the background information

Time:11-29

In the autumn weather, I go to travel, "" {" data" : [], "code" : 1, the "message" : "trickling water 嬭 鎴 愬 姛"}

"" I uploaded the file content on the left, the right is more content, after I downloaded file format for the TXT format

Below to download the complete code

 public static void dowFile (String fileName, HttpServletResponse response) throws the Exception {
If (fileName!=null) {
//set the file path
The File File=new File (ClassUtils getDefaultClassLoader (). The getResource (" "). GetPath () + "static \ " + fileName);
Response. SetContentType (" application/force - download "); Don't open//set the mandatory download
String jieguo=fileName. The substring (fileName. IndexOf (" ~ ") + 1);
The response. AddHeader (" the Content - the Disposition ", "legislation; FileName="+ URLEncoder. Encode (jieguo" utf-8 "));//set the file name
Byte [] buffer=new byte [1024].
FileInputStream fis=null;
BufferedInputStream bis=null;
Try {
Fis=new FileInputStream (file);
The bis=new BufferedInputStream (fis);
OutputStream OS=response. GetOutputStream ();
Int I=bis. Read (buffer);
While (I!=1) {
OS. Write (buffer, 0, I);
I=the bis. Read (buffer);
}
} the catch (Exception e) {
Throw new BusinessException (download "failure");
} the finally {
If (bis!=null) {
Try {
The bis. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
If (fis!=null) {
Try {
Fis. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
Throw new BusinessException (" no the file ");
}

CodePudding user response:

Good is very useful
  • Related