最初の方法:ローカルに保存
パッケージcom.cnki.tool.base; import javax.servlet.http.HttpServletResponse; import java.io. * ; import java.util.ArrayList; import java.util.List; public class ExportTxtUtil { / ** * Export * * @param file * Txtファイル(パス+ファイル名)、Txt ファイルが存在しない場合は自動的に作成されます * @param dataList * data * @return * / public static boolean exportTxt(File file、List <String> dataList){ FileOutputStream out= null ; { out = new FileOutputStream(file);を試してください。 リターンexportTxtByOS(うち、DataListコントロール); } catch (FileNotFoundException e){ e.printStackTrace(); falseを返します。 } } / ** *导出 * * @param out *输出流 * @param dataList *数データ * @return * / public static boolean exportTxtByOS(OutputStream out、List <String> dataList){ boolean isSucess = false ; OutputStreamWriter osw = null ; BufferedWriter bw = null ; { osw = new OutputStreamWriter(out);を試してください。 bw = new BufferedWriter(osw); // 循环数据ため(INT I = 0、I <dataList.size(); I ++ ){ 。bw.append(dataList.get(i))を追記( "\ R \ n" ); } isSucess = true ; } catch (例外e){ e.printStackTrace(); isSucess = false ; } 最後に{ if(bw!= null ){ try { bw.close(); bw = null ; } catch (IOException e){ e.printStackTrace(); } } if(osw!= null){ { osw.close();を試す osw = null ; } catch (IOException e){ e.printStackTrace(); } } if(out!= null ){ try { out.close(); out = null ; } catch (IOException e){ e.printStackTrace(); } } } 戻り値はisSucess; } public static void main(String [] args){ リスト <String>リスト= 新しい ArrayList <String> (); list.add( "Hello、World!" ); list.add( "Hello、World!" ); list.add( "Hello、World!" ); list.add( "Hello、World!" ); 文字列filePath = "D:/ txt /" ; String fileName = java.util.UUID.randomUUID()。toString()。replaceAll( "-"、 "")+ "。txt" ;File(filePath); if(!pathFile.exists()){ pathFile.mkdir(); } 文字列relFilePath = filePath + File.separator + fileName; ファイルfile = new File(relFilePath); if(!file.exists()){ file.createNewFile(); } boolean isSuccess = exportTxt(file、list); System.out.println(isSuccess); } }
ブラウザーを直接ダウンロードする2番目の方法:
/ * 拼接字符串 * @author * @param * @return * / @RequestMapping( "exportLog.do" ) public void exportLog(HttpServletResponse response){ // 获取日志 List <DtmSystemLog> list = logService.getLogs(); // 拼接字符串 StringBuffer text = new StringBuffer(); for (DtmSystemLog log:list){ text.append(log.getOpeuser()); text.append( "|" ); text.append(log.getOpedesc()); text.append( "|"); text.append(dateString); text.append( "\ r \ n"); // 改行文字 } exportTxt(response、text.toString()); } / * txtファイルをエクスポート * @author * @param response * @param text export string * @return * / public void exportTxt(HttpServletResponse response、String text){ response.setCharacterEncoding( "utf-8" ); // 応答コンテンツタイプを設定し ますresponse.setContentType( "text / plain " ); // ファイル名と形式を設定する response.addHeader( "Content-Disposition"、 "attachment; filename =" + genAttachmentFileName( "File name"、 "JSON_FOR_UCC_")// 名前形式を設定します。この中国語名がないと表示できません + ".txt" ); BufferedOutputStream buff = null ; ServletOutputStream outStr = null ; try { outStr = response.getOutputStream(); buff = new BufferedOutputStream(outStr); buff.write(text.getBytes( "UTF-8" )); buff.flush(); buff.close (); } キャッチ(Exception e){ // LOGGER.error( "Error exporting file:{}"、e); } 最後に { try { buff.close(); outStr.close(); } catch (Exception e){ / / LOGGER.error( "ストリームオブジェクトを閉じるエラーe:{}"、e); } } } // 中国語のファイル名の表示エラーを防止します public String genAttachmentFileName(String cnName、String defaultName){ try { cnName = new String(cnName。 getBytes( "gb2312")、 "ISO8859-1" ); } キャッチ(例外e){ cnName = defaultName; } cnNameを返します。 }
リモートでブラウザーを呼び出してtxtファイルをダウンロードする方法:
org.apache.commons.io.IOUtilsを インポートします。 @RequestMapping({ "/demp/common/downloadDeviceLog.do" }) @ResponseBody public void downloadDeviceLog(HttpServletRequest request、HttpServletResponse response)throws Exception { String logUrl = "https://**/2018-11-20.txt" ; 試す{ 文字列[] logUrlArray = logUrl.split( "/" )。 String fileName = logUrlArray [logUrlArray.length-1 ]; URL url = 新しいURL(logUrl); URLConnection uc =url.openConnection(); response.setContentType( "application / octet-stream"); // 设置文件类型 response.setHeader( "content-disposition"、 "attachment; filename =" + URLEncoder.encode(fileName、 "UTF-8" )); response.setHeader( "Content-Length" 、String.valueOf(uc.getContentLength())); ServletOutputStream out = response.getOutputStream(); IOUtils.copy(uc.getInputStream()、out); } catch (例外e){ e.printStackTrace(); } }