5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring bootでCSVダウンロード

Last updated at Posted at 2018-03-07

はじめに

SpringbootでCSVダウンロード機能を実装しようとするといくつか方法が見つかったのですがSpringで実行する仮想のTomcatではうごいたのに、実際にサーバに持って行ってTomcatに乗せてみると動かなかったりしたのでまとめてみました。

環境

  • apache Tomcat
  • Spring-Boot 1.5.9
  • Java 8

ソースコード

  • HTML, javaScript(ボタン押すだけです)
.html
<form id="csvform" method="post">
    <button type="button" onClick="downloadCSV();"><img id="MORDAL_OKBUTTON_ICON"></img>確定</button>
</form>
<script>
function downloadCSV(){
	var date = new Date();
	var file_title = addzero(date.getFullYear())+addzero(date.getMonth()+1)+addzero(date.getDate())+addzero(date.getHours())+addzero(date.getMinutes())+addzero(date.getSeconds());
	url= "csvdownload";
	$('#csvform').attr('action', url);
	$('#csvform').attr('method', "GET");
	$('#csvform').submit();
}

function addzero(text){
	text = String(text);
	if(text.length==1){
		text = "0"+text;
	}
	return text;
}
</script>

  • java
.java

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class CsvController {

	@RequestMapping(value = "/csvdownload/csv", method = RequestMethod.GET)
	public ResponseEntity<byte[]> download(
    		@RequestParam("csvsearchname") String csvsearchname) throws IOException {
	    HttpHeaders h = new HttpHeaders();
	    h.add("Content-Type", "text/csv; charset=MS932");
	    String  filename = csvsearchname;
	    h.setContentDispositionFormData("filename", filename + ".csv");
    	String CSVData = null;
    	try{
	    	StringBuilder CSV = new StringBuilder();

	    	CSV.append("ssss");
	    	CSV.append("aaaa");
	        CSVData =CSV.toString();

    	}catch(Exception e){
    		System.out.println(e);
    		CSVData = null;
    	}
	    return new ResponseEntity<>(CSVData.getBytes("MS932"), h, HttpStatus.OK);
	}
}

終わりに

CSVダウンロード機能は様々なシステムに入っており、よく使われる機能ですが、

  • 一度ファイルを生成して、ダウンロードさせる
  • byteデータのまま返却する(今回の方法)
  • etc
    と様々な方法があると思うので、開発環境とやりたいことにあった方法を選択していきたいですね。
5
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?