[SpringBoot2.4.3] uploadを任意の場所にファイルを保存

Controller.java
L new FileOutputStream(${path})で、保存場所を指定する

	@RequestMapping(value="upload", method=RequestMethod.POST)
    public void handle(
            HttpServletResponse response,
            @RequestParam MultipartFile file
            ) {
        if(file.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        try {
            BufferedInputStream in = new BufferedInputStream(file.getInputStream());
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream("./target/" + file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException("Error uploading file.", e);
        }
    }

なるほど、resourcesのstaticの中に置くこともできますね。

new FileOutputStream(“./src/main/resources/static/file/” + file.getOriginalFilename())

### filenameをreturn
res.getWriter().write(file.getOriginalFilename());

OK、後は基本機能としてはログインのみなんだよなー

[SpringBoot2.4.3] ファイルアップロードのcontroller

import javax.servlet.http.HttpServletResponse;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/contract")
public class ContractController {

	@GetMapping("input")
	public String contract() {
		return "contract/input";
	}
	
	@RequestMapping(value="upload", method=RequestMethod.POST)
    public void handle(
            HttpServletResponse response,
            @RequestParam MultipartFile file
            ) {
        if(file.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        try {
            BufferedInputStream in = new BufferedInputStream(file.getInputStream());
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException("Error uploading file.", e);
        }
    }
}

html

<form action="/contract/upload" method="post" enctype="multipart/form-data">
    <div class="form-group">
    <div class="custom-file">
      <input type="file" name="file" class="custom-file-input" id="inputFile">
    </div>
  </div>
  <br>
<button type="submit" class="btn btn-primary" id="upload" value="upload">登録</button>	
</form>

BufferedInputStreamとBufferedOutputStreamの使い方がイマイチよくわからんな。。

[SpringBoot2.4.3] ファイルアップロード機能を作る

まず、webpackで簡単にフロントを作成します

続いて、そのまま、src/main/resources/templates/contract/input.html に流し込みます。

<form action="/upload" enctype="multipart/form-data">
    <div class="form-group">
    <div class="custom-file">
      <input type="file" name="file" class="custom-file-input" id="inputFile">
      <!-- <label class="custom-file-label" for="inputFile"></label> -->
    </div>
  </div>
  <br>
<button type="button" class="btn btn-primary">登録</button>	
</form>

続いてControllerを作ります。適当にContractController.javaとします。

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/contract")
public class ContractController {

	@GetMapping("input")
	public String contract() {
		return "contract/input";
	}
}

ここからfile保存を実装したい。。。

[SpringBoot2.4.3] Service

package com.example.demo;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Service;

@Service
public class MyDataService {
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public List<MyData> getAll(){
		return (List<MyData>) entityManager
				.createQuery("from MyData").getResultList();
	}
	
	public MyData get(int num) {
		return (MyData)entityManager
				.createQuery("from MyData where id = " + num)
				.getSingleResult();
	}
	
	public List<MyData> find(String fstr){
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<MyData> query = builder.createQuery(MyData.class);
		Root<MyData> root = query.from(MyData.class);
		query.select(root).where(builder.equal(root.get("name"), fstr));
		List<MyData> list = null;
		list = (List<MyData>) entityManager.createQuery(query).getResultList();
		return list;
	}
}

[SpringBoot2.4.3] テストアプリケーションを作る

gradleで作ります。

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-mustache'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.flywaydb:flyway-core'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'org.postgresql:postgresql'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

YAMLファイルの作成
src/main/resources/application.yml

spring:
  datasource:
    url:jdbc:postgresql://localhost:5432/test
    driverClassName:org.postgresql.Driver
    username:root
    password:
  mvc:
   favicon:
     enabled:false

ん? Nullにするとエラーになるな

V1__Create.sql

create table tsubuyaki (
	id serial primary key,
	txt varchar(100) not null,
	version integer not null default 0,
	updated_time timestamp not null default current_timestamp,
	created_time timestamp not null default current_timestamp
);

Model

package com.example.demo.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
public class Tsubuyaki extends TimestampEntity {
	
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public long id;
	
	@NotEmpty
	public String txt;
	
	@Version
	public long version;
}
package com.example.demo.model;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

@MappedSuperclass
public abstract class TimestampEntity {
	
	public Timestamp updatedTime;
	
	@Column(updatable=false)
	public Timestamp createdTime;
	
	@PrePersist
	public void prePersist() {
		Timestamp ts = new Timestamp((new Date()).getTime());
		this.createdTime = ts;
		this.updatedTime = ts;
	}
	
	@PreUpdate
	public void preUpdate() {
		this.updatedTime = new Timestamp((new Date()).getTime());
	}
}

Repository

package com.example.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.example.demo.model.Tsubuyaki;

public interface TsubuyakiRepository extends
CrudRepository<Tsubuyaki, Long>{
	Iterable<Tsubuyaki> findAllByOrderByUpdatedTimeDesc();
}

controller

package com.example.demo.controller;

import java.util.Collections;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Tsubuyaki;
import com.example.demo.repository.TsubuyakiRepository;

@RestController @RequestMapping("/tsubuyaki")
public class TsubuyakiController {
	
	@Autowired TsubuyakiRepository repo;
	
	@RequestMapping(method=RequestMethod.POST)
	public Map<String, Tsubuyaki> create(
			@Valid @RequestBody Tsubuyaki tsubuyaki
			){
			return Collections.singletonMap(
					"tsubuyaki", repo.save(tsubuyaki));
		
	}
	@RequestMapping(method=RequestMethod.GET)
	public Map<String, Tsubuyaki> read(){
			return Collections.singletonMap(
					"tsubuyaki", repo.findAllByOrderByUpdatedTimeDesc());
		
	}
	@RequestMapping(path="/{id}", method=RequestMethod.PUT)
	public void update(
			@PathVariable Long id, @RequestParam String txt
			){
			Tsubuyaki tsubuyaki = repo.findOne(id);
			tsubuyaki.txt = txt;
			repo.save(tsubuyaki);
	}
	@RequestMapping(path="/{id}", method=RequestMethod.DELETE)
	public void delete(
			@PathVariable Long id
			){
			repo.delete(id);
	}
	
}

ぐぬぬぬ。。。

[SpringBoot2.4.3] Scheduling

Controller

package com.example.demo;

import java.util.Date;
import java.text.SimpleDateFormat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class HelloController {
	
	private static final SimpleDateFormat
	fmt = new SimpleDateFormat("HH:mm:ss");
	
	@Scheduled(fixedRate = 5000)
	public void reportTime() {
		System.out.println(fmt.format(new Date()));
	}
	
	public static void main(String[] args) {
		SpringApplication.run(HelloController.class, args);
	}
}

==========================
CONDITION EVALUATION DELTA
==========================

Positive matches:
—————–

TaskSchedulingAutoConfiguration#taskScheduler matched:
– @ConditionalOnBean (names: org.springframework.context.annotation.internalScheduledAnnotationProcessor; SearchStrategy: all) found bean ‘org.springframework.context.annotation.internalScheduledAnnotationProcessor’; @ConditionalOnMissingBean (types: org.springframework.scheduling.annotation.SchedulingConfigurer,org.springframework.scheduling.TaskScheduler,java.util.concurrent.ScheduledExecutorService; SearchStrategy: all) did not find any beans (OnBeanCondition)

Negative matches:
—————–

None

Exclusions:
———–

None

Unconditional classes:
———————-

None

16:02:28
16:02:33
16:02:38
16:02:43
16:02:48
16:02:53
16:02:58
16:03:03
16:03:08
16:03:13
16:03:18
16:03:23
16:03:28
16:03:33
16:03:38

なんやこれは、すげえ

[SpringBoot2.4.3] MessageSource

application.propertiesの設定
メッセージを messages_ja.properties で設定できるようにする
application.properties

spring.messages.basename=messages
spring.messages.cache-seconds=-1
spring.messages.encoding=UTF-8

messages_ja.properties

key=\u3053\u3093\u306B\u3061\u306F\u3002

Controller

String message = msg.getMessage("key",null,Locale.JAPAN);

@RequestMapping(value="/msg", method=RequestMethod.GET)
public Map<String, String> msg(Locale locale){
	String message = msg.getMessage("key", null, locale);
	return Collections.singletonMap("message", message);
}

なるほどー

[SpringBoot2.4.3] Mustacheを使う

MustacheとはSpring Bootのテンプレートエンジンです。

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mustache</artifactId>
		</dependency>

controller

package com.example.demo;

import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {
	
	@RequestMapping("/hello-mst")
	public String hello(
			@RequestParam(defaultValue="World")String name,
			Map<String, Object> model
			) {
		model.put("name", name);
		model.put("date", new Date());
		return "hello-mst";
	}
}

html

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Hello Mustache</title>
</head>
<body>
	<div>
		<p><b>Message:</b>Hello, {{name}}</p>
		<p><b>Date:</b>{{date}}</p>
	</div>
</body>
</html>

あら、うまくいかんね。

[SpringBoot2.4.3] バリデーション

package com.example.demo;

import javax.validation.Valid;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	@RequestMapping(value="/address", method=RequestMethod.POST)
	public Address create(@Valid @RequestBody Address address) {
		return address;
	}
	
	public static class Address {
		
		@NotEmpty
		@Size(min=7, max=7)
		public String zip;
		
		@NotEmpty
		public String address;
	}
}

[SpringBoot2.4.3] ファイルアップロード

htmlファイル
src/main/resources/public/file-upload.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>File Upload</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
</head>
<body>
<div class="">
	<form id="form" enctype="multipart/form-data">
		<p><input type="file" name="file"></p>
		<p><input type="button" id="upload" value="upload"></p>
	</form>
	<span id="result" style="padding:3px"></span>

</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/js/bootstrap.min.js"></script>
<script
  src="https://code.jquery.com/jquery-3.5.1.js"
  integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
  crossorigin="anonymous"></script>
<script>
$(function(){
	$('#upload').click(function(){
		var formData = new FormData(
			$('#form').get()[0]
		);
		$.ajax({
			url:'/upload',
			method:'post',
			data:formData,
			processData:false,
			contentType:false,
			cache: false
		}).done(function(data,status,jqxhr){
			$('#result').text('結果: 成功');
		}).fail(function(data, status, jqxhr){
			$('#result').text('結果: 失敗');
		});
	});
});
</script>
</body>
</html>

Controller

package com.example.demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class HelloController {
	
	@RequestMapping(value="/upload", method=RequestMethod.POST)
	public void handle(
			HttpServletResponse response,
			@RequestParam MultipartFile file
			) {
		if(file.isEmpty()) {
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return;
		}
		try {
			BufferedInputStream in = new BufferedInputStream(file.getInputStream());
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(file.getOriginalFilename()));
		} catch (IOException e) {
			throw new RuntimeException("Error uploading file.", e);
		}
	}

publicに置くと、routingしなくて良いのか。。

ほう、こんなんなってるのか。。