LoginSignup
1
1

More than 1 year has passed since last update.

Spring入門-URL叩いてファイル作成

Last updated at Posted at 2022-08-14

Java界隈で良く使われているSpringBootでファイル作成処理を作っていきます。
処理概要図.png

UserがリクエストURL「localhost:8080/api」を叩いたら、任意の場所にファイル作成を行う。
Springプロジェクトだと「src/main/resources」に置くことが多い。
※参照ファイルは大抵の場合、resourcesに入っている

HelloController.java
package com.sample.app.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.controller.service.OutputStreamSample;

/** サンプルコントローラー */
@RestController
public class HelloController {

	/** ファイル作成処理サンプル */
	@Autowired
	OutputStreamSample outputStreamSample;

	private final String message = "ファイルが作成されました(゚∀゚)";

	/**
	 * getメソッドで処理を実行(http://localhost:8080/api)
	 *
	 * @throws IOException
	 * @return 表示メッセージ
	 */
	@GetMapping("api")
	public String hello() throws IOException {

		// ここにファイル作成処理を実行する。
		outputStreamSample.createFile();

		return message;
	}
}
OutputStreamSample.java
package com.sample.app.controller.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

/** ファイル作成処理サンプル */
@Service
public class OutputStreamSample {
	/**
	 * ファイル作成処理
	 * 
	 * @throws IOException
	 */
	public void createFile() throws IOException {
		// ファイルの中身を記載
		String exampleString = "HelloWorld\nようこそSpringBootの世界へ\nこれが作成されたファイルの内容だよ\n⊂⌒~⊃。Д。)⊃";
		// ライブラリ「org.apache.commons.io」を利用して String → InputStream を作成
		// toInputStream(InputStream, 文字コードの指定);
		InputStream inputStream = IOUtils.toInputStream(exampleString, StandardCharsets.UTF_8);
		// ファイル生成
		String resourcesPath = "src/main/resources/output/";
		FileOutputStream outputStream = new FileOutputStream(new File(resourcesPath + "samplefile.txt"));
		// ファイル書き込み処理
		int mojiIndex;
		while ((mojiIndex = inputStream.read()) != -1) {
			char getSingleChar = (char) mojiIndex;
			outputStream.write(getSingleChar);
		}
	}
}

・動作内容
「localhost:8080/api」を叩くと...
image.png
と表示される。

作成されたファイルの中身は..

samplefile.txt
HelloWorld
ようこそSpringBootの世界へ
これが作成されたファイルの内容だよ
⊂⌒~⊃。Д。)⊃

こんな感じです。

・ライブラリ「org.apache.commons.io」について

InputStream inputStream = IOUtils.toInputStream(exampleString, StandardCharsets.UTF_8);

ここの一文で使用されている「IOUtils.toInputStream」ライブラリーを用いて、文字データをファイルデータに置き換えている。

gradleでインストール際は以下の記述が必要。

dependencies {
	implementation 'commons-io:commons-io:2.11.0'
}

・参考サイト
『Java で文字列を InputStream に変換する』
https://www.delftstack.com/ja/howto/java/java-string-to-inputstream/
『Apache Commons IO ™』
https://commons.apache.org/proper/commons-io/dependency-info.html
『Gradle Docs』
https://docs.gradle.org/4.10/userguide/java_plugin.html#sec:java_plugin_and_dependency_management

・動作確認用の公開ソースコード
https://github.com/sunn-sudo/spring-sample

1
1
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
1
1