Spring Bootで画像を縮小して表示
画像を縮小する
そもそも画像制御とかプログラムでほとんど書かないけどね
実装
Image01Controller.java
package com.example.controller; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.core.io.ResourceLoader; import javax.imageio.ImageIO; @RestController public class Image01Controller { @Autowired ResourceLoader resourceLoader; private int c; @ResponseBody @RequestMapping(value = "/getImageReducing/{id}", method = {RequestMethod.GET }) public HttpEntity<byte[]> getImageReducing(@PathVariable String id) throws IOException { // リソースファイルを読み込み Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg"); InputStream image = resource.getInputStream(); // BufferedImageへ設定 BufferedImage bufferedImage1 = ImageIO.read(image); BufferedImage bufferedImage2 = null; // 変換情報を設定 int width = 300; int height = 250; Image image1 = bufferedImage1.getScaledInstance(width, height, Image.SCALE_DEFAULT); bufferedImage2 = new BufferedImage(image1.getWidth(null), image1.getHeight(null), BufferedImage.TYPE_INT_RGB); // 縮小処理 Graphics graphics2 = null; try { graphics2 = bufferedImage2.getGraphics(); int x = 0; int y = 0; graphics2.drawImage(bufferedImage1, x, y, width, height, null); } finally { graphics2.dispose(); } // byteへ変換 ByteArrayOutputStream bout = new ByteArrayOutputStream(); ImageIO.write(bufferedImage2, "jpg", bout); byte[] b = bout.toByteArray(); // レスポンスデータとして返却 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); headers.setContentLength(b.length); return new HttpEntity<byte[]>(b, headers); } }
Spring BootでRestControllerで画像を出力する
簡単にresources配下にあるファイルを画面に表示する
Spring Bootの場合は@RestControllerを返す場合は指定のデータを返却するような感じになるので返り値をバイナリ形式で返してみる。
やること
- 画像を読み込み
- byte[]へ変換して設定
- jpegデータとして返却
実装
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
Image01Controller.java
package com.example.controller; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.core.io.ResourceLoader; @RestController public class Image01Controller { @Autowired ResourceLoader resourceLoader; @ResponseBody @RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET }) public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException { // リソースファイルを読み込み Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg"); InputStream image = resource.getInputStream(); // byteへ変換 byte[] b = IOUtils.toByteArray(image); // レスポンスデータとして返却 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); headers.setContentLength(b.length); return new HttpEntity<byte[]>(b, headers); } }
参考
Spring MVC: How to return image in @ResponseBody? - Stack Overflow
Java + Spring Boot : Downloading image and pass it to a request - Stack Overflow
Java - spring bootで静的ファイルにアクセスする方法(25083)|teratail
わかったこと
今回はここまで…
おまけ
ByteArrayOutputStreamでbyte[]変換した場合
package com.example.controller; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.core.io.ResourceLoader; @RestController public class Image01Controller { @Autowired ResourceLoader resourceLoader; private int c; @ResponseBody @RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET }) public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException { // リソースファイルを読み込み Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg"); InputStream image = resource.getInputStream(); // byteへ変換 ByteArrayOutputStream bout = new ByteArrayOutputStream(); int c; while ((c = image.read()) != -1) { bout.write(c); } byte[] b = bout.toByteArray(); // レスポンスデータとして返却 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); headers.setContentLength(b.length); return new HttpEntity<byte[]>(b, headers); } }
Spring BootのRestTemplateでバイナリデータを取得する
RestTemplate
Spring BootのAPI処理クラスと思われる
動作用API
LINE APIのGet ContentAPIを使用
https://devdocs.line.me/ja/?shell#get-content
実装サンプル
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
App
** RestTemplate Spring BootのAPI処理クラスと思われる ** 実装サンプル *** App >|java| package com.example; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import org.apache.commons.io.IOUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class SampleSpringResttemplate01Application { public static void main(String[] args) throws IOException { SpringApplication.run(SampleSpringResttemplate01Application.class, args); // インスタンス生成 RestTemplate restTemplate = new RestTemplate(); // メッセージコンバーター設定 restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); // ヘッダー設定 HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); headers.add("Authorization", "Bearer " + "アクセストークン"); // パラメータ設定 HttpEntity<String> entity = new HttpEntity<String>(headers); // URL String url; url = "https://api.line.me/v2/bot/message/999999999999999/content"; // リクエスト発行 ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class, "1"); // 戻り値よりチェック if(response.getStatusCode().equals(HttpStatus.OK)) { FileOutputStream output = new FileOutputStream(new File("filename.jpg")); IOUtils.write(response.getBody(), output); } } }
参考
java - Basic authentication for REST API using spring restTemplate - Stack Overflow
Read file/banary stream from the output of Spring resttemplate – TechTalk
所感
パッケージだけの場合はどのクラスかわかりにくいので動かすのがちょっと手間取ります。まあ、サンプルとして参考にさせていただいているのでわがままはいえませんけどw
Spring Bootでメールを送信する
Spring Bootでメールを送信する
今回の送信は簡単な平分を送信するだけ
メールサーバ
Gmailをリレーする
設定
application.properties
# port server.port=7777 # mail spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=Gmailのメールアドレス spring.mail.password=Gmailのパスワード spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
※Spring Bootではここに値を設定する
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
メール送信
MailSampleController.java
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MailSampleController { private final JavaMailSender javaMailSender; @Autowired MailSampleController(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } @RequestMapping(value = "/mail/send", method = {RequestMethod.POST} ) public String send() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo("送信先のメールアドレス"); mailMessage.setReplyTo("リプライのメールアドレス"); mailMessage.setFrom("Fromのメールアドレス"); mailMessage.setSubject("テストメール"); mailMessage.setText("テストメールです、\nここから次の行\nおわりです\n"); javaMailSender.send(mailMessage); return "ok"; } }
このapiを叩けばメールを送信する。
結果
所感
メール送信自体は今までなんどもあったけどローカルの開発環境で送信する手段があまり準備されていないことが多くて準備にドタバタしている感じになってしまう。テストとかする場合はもあるけどとりあえずはこれで…
MacよりGmailへメールを送信
備忘録
メッセージのソースだけだとあまりわからないので…
Postfix設定
設定ファイルバックアップ
sudo cp -p /etc/postfix/main.cf /etc/postfix/main.cf.org
/etc/postfix/main.cf
# リレーホストを追加 relayhost = [smtp.gmail.com]:587 #sasl setting smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous smtp_sasl_mechanism_filter = plain #tls setting smtp_use_tls = yes
※末尾に追加
/etc/postfix/sasl_passwd
[smtp.gmail.com]:587 ユーザーID@gmail.com:パスワード
※ユーザーIDとパスワードはそれぞれ任意のものを設定
※2段階認証している場合はアプリパスワードを発行すること
再作成
sudo chmod 640 /etc/postfix/sasl_passwd sudo postmap /etc/postfix/sasl_passwd
再読込
sudo postfix reload
テスト
echo hogehoge | mail ユーザーID@gmail.com
macのpostfixでローカルメールを扱う
基本的には
postfix
存在チェック
$ which postfix /usr/sbin/postfix $
あるのでそのまま
起動
sudo postfix start
設定ファイルバックアップ
sudo cp -p /etc/postfix/main.cf /etc/postfix/main.cf.org
/etc/postfix/main.cf編集
# 全てのメールを受け取る inet_interfaces = all # ローカル配送で不明なユーザを拒否しない local_recipient_maps = # Maildir形式として保存するディレクトリを /usr/local/mail/ の下にユーザー毎に作成する mail_spool_directory = /usr/local/mail/ # ローカル配送で不明なユーザへのメールは maildev へ送る luser_relay = maildev # トランスポートマップを指定 transport_maps = hash:/etc/postfix/transport
※末尾に追加
postmapで作成
sudo postmap /etc/postfix/transport
メール保存先を準備
sudo mkdir -p /usr/local/mail/ sudo chmod 777 /usr/local/mail/
ユーザーを作成
sudo dscl . -create /Groups/users gid 1001 sudo dscl . -create /Users/maildev sudo dscl . -create /Users/maildev RealName maildev sudo dscl . -create /Users/maildev UniqueID 1001 sudo dscl . -create /Users/maildev PrimaryGroupID 1001 sudo dscl . -create /Users/maildev NFSHomeDirectory /Users/maildev sudo dscl . -create /Users/maildev UserShell /bin/bash sudo passwd maildev sudo createhomedir -b -u maildev
※500番台を使用するとシステムが使えなくなる恐れがあるので気をつける必要があります。
リロード
sudo postfix reload
dovecot確認
brew install
$ brew install dovecot
READMEを確認
$ cat /usr/local/etc/dovecot/README Configuration files go to this directory. See example configuration files in /usr/local/Cellar/dovecot/2.2.27/share/doc/dovecot/example-config/ $
移動
cp /usr/local/Cellar/dovecot/2.2.27/share/doc/dovecot/example-config/dovecot.conf /usr/local/etc/dovecot/ cp -r /usr/local/Cellar/dovecot/2.2.27/share/doc/dovecot/example-config/conf.d /usr/local/etc/dovecot/
起動
sudo brew services start dovecot
/usr/local/etc/dovecot/dovecot.conf
protocols = imap pop3 mail_location = maildir:/usr/local/mail/%u
再起動
sudo brew services restart dovecot
テスト
$ telnet localhost 25 Trying ::1... Connected to localhost. Escape character is '^]'. 220 matsumotoshigejinoMac-mini.local ESMTP Postfix helo localhost 250 matsumotoshigejinoMac-mini.local mail from: aaaa@test1.com 250 2.1.0 Ok rcpt to: bbb@hoge.com 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> subject: test2 aaaaaa ssssssssss ddddd . 250 2.0.0 Ok: queued as 5AA2D4F6580 quit 221 2.0.0 Bye Connection closed by foreign host. $
メール確認
$ ll /usr/local/mail/maildev/new/ total 8 -rw------- 1 maildev 601 443 1 7 02:21 1483723300.V1000008I4f6595M15495.matsumotoshigejinoMac-mini.local $
ソース内容
Return-Path: <aaaa@test1.com> X-Original-To: bbb@hoge.com Delivered-To: bbb@hoge.com Received: from localhost (localhost [IPv6:::1]) by matsumotoshigejinoMac-mini.local (Postfix) with SMTP id 5AA2D4F6580 for <bbb@hoge.com>; Sat, 7 Jan 2017 02:21:02 +0900 (JST) subject: test2 Message-Id: <20170106172111.5AA2D4F6580@matsumotoshigejinoMac-mini.local> Date: Sat, 7 Jan 2017 02:21:02 +0900 (JST) From: aaaa@test1.com aaaaaa ssssssssss ddddd
こんな感じ?
Windows10で開発用のSMTPサーバを使いたい
環境
Windows10(VM)
設定
smtp4dev 2.0.9 standaloneをダウンロード
ダウンロードしたファイルを解凍して起動する
Windowsの機能が足りない場合は追加する
ファイアウォールを解除する
起動を確認
メール送信
JavaMailで送信する
package maven_test01; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Sample01 { public static void main(String[] args) throws AddressException, MessagingException { Properties props = new Properties(); //smptサーバに関する設定 props.setProperty("mail.smtp.host", "localhost"); props.setProperty("mail.smtp.port", "25"); Session session = Session.getInstance(props, null); session.setDebug(true); MimeMessage msg = new MimeMessage(session); // 送信データ設定 msg.setFrom(new InternetAddress("test@example.com")); InternetAddress[] address = {new InternetAddress("hoge@example.com")}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("JavaMail APIs Test"); msg.setSentDate(new Date()); msg.setText("hogehoge"); // 送信 Transport.send(msg); } }