feat: Blob 파일 DB 저장 예제

dev
gitea-관리자 1 year ago
parent f1e9ccd1d9
commit 3ad678f6d7

@ -2,7 +2,6 @@ package kr.xit.core.spring.util;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
@ -47,12 +46,16 @@ public class BlobInsert {
"""; """;
int idx = 1; int idx = 1;
PreparedStatement statement = conn.prepareStatement(sql); PreparedStatement statement = conn.prepareStatement(sql);
File file = new File(filePath); File file = new File(filePath);
if(file.exists()) { if(!file.exists()) {
BizRuntimeException.create(String.join(StringUtils.EMPTY, filePath, " 파일이 존재하지 않습니다.")); throw BizRuntimeException.create(String.join(StringUtils.EMPTY, filePath, " 파일이 존재하지 않습니다."));
}
double size = Math.ceil(file.length()/1024.0 * 10)/10;
if(size > 300.0){
throw BizRuntimeException.create(String.format("파일 크기 %s[템플릿 파일 크기는 300kb를 넘을 수 없습니다]", String.valueOf(size)));
} }
System.out.println(file.getName());
System.out.println(file.getCanonicalPath());
try (InputStream inputStream = new FileInputStream(file)) { try (InputStream inputStream = new FileInputStream(file)) {
statement.setBlob(idx++, inputStream); statement.setBlob(idx++, inputStream);
@ -61,18 +64,16 @@ public class BlobInsert {
statement.setString(idx++, "ME111"); statement.setString(idx++, "ME111");
int row = statement.executeUpdate(); int row = statement.executeUpdate();
if (row > 0) {
System.out.println("A contact was inserted with photo image.");
}
conn.commit(); conn.commit();
}catch (FileNotFoundException fe){
fe.printStackTrace(); }catch (IOException e) {
throw BizRuntimeException.create(e.getMessage());
} }
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); throw BizRuntimeException.create(ex.getMessage());
} catch (IOException ex) {
ex.printStackTrace();
} }
} }
} }

Loading…
Cancel
Save