// element-uiビュー
//背景java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
org.apache.commons.io.IOUtilsをインポートします。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownLoadFile {
プライベート静的ロガーログ= LoggerFactory.getLogger(DownLoadFile.class);
@RequestMapping(value = "/ downLoad"、method = RequestMethod.GET)
public static final String downLoad(HttpServletResponse res)throws UnsupportedEncodingException {
Map <String、Object> reMap = new HashMap <>();
//ファイル名は仮パラメータで渡すことができる
String fileName = "t_label.txt";
//ダウンロードするファイルアドレスは、仮パラメータで渡すことができる
String filepath = "f:/ svs /" + fileName;
OutputStream os = null;
InputStream is = null;
try {
//出力ストリームを取得
os = res.getOutputStream();
//出力ストリームをクリア
res.reset();
res.setContentType( "application / x-download; charset = GBK ");
= null){ is.close(); }
}キャッチ(IOException e){
log.error(e.toString());
}
試行{
if(os!= null){
os.close();
}
} catch(IOException e){
log.error(e.toString());
}
}
文字列str = JsonUtil.map2Json(reMap);
strを返します。
}
}
注:このコードは、Firefoxブラウザーでテストした場合は問題ありません。Googleのバージョンと設定が異なる場合、ファイルのダウンロードエラーが発生し、[名前を付けて保存]ダイアログボックスが直接ポップアップしなくなることもあります。