Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#30 웹소켓을 이용한 1:1 채팅 구현
Browse files Browse the repository at this point in the history
- DateUtil 생성
  yyyyMMddHHmm 형태 문자열 반환, 확인 메서드
- FileOutputStream, FileChannel 사용시 try-with-resource 적용
  • Loading branch information
junshock5 committed Sep 3, 2020
1 parent 8d887b5 commit 5fb8d10
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/main/java/com/market/server/handler/SocketHandler.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.market.server.handler;
import com.market.server.utils.DateUtil;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.json.simple.JSONObject;
Expand All @@ -15,7 +16,6 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -68,37 +68,23 @@ public void handleBinaryMessage(WebSocketSession session, BinaryMessage message)
int fileUploadIdx = 0;
//바이너리 메시지 발송
ByteBuffer byteBuffer = message.getPayload();

String fileName = new SimpleDateFormat("yyyyMMddHHmm'.jpg'").format(new Date());
String fileName = DateUtil.getNowTimeToyyyyMMddHHmm(new Date(), ".jpg");
String fileDirectoryName = System.getProperty("user.dir") + File.separator + "attachFile" + File.separator;
File file = new File(fileDirectoryName, fileName);

File dir = new File(fileDirectoryName);
if (!dir.exists()) {
dir.mkdirs();
}

File file = new File(fileDirectoryName, fileName);
FileOutputStream out = null;
FileChannel outChannel = null;
try {
try (FileOutputStream out = new FileOutputStream(file, true); //생성을 위해 OutputStream을 연다.
FileChannel outChannel = out.getChannel(); //채널을 열고)
) {
byteBuffer.flip(); //byteBuffer를 읽기 위해 세팅
out = new FileOutputStream(file, true); //생성을 위해 OutputStream을 연다.
outChannel = out.getChannel(); //채널을 열고
byteBuffer.compact(); //파일을 복사한다.
outChannel.write(byteBuffer); //파일을 쓴다.
} catch (Exception e) {
log.error("파일 write 실패", e);
} finally {
try {
if (out != null) {
out.close();
}
if (outChannel != null) {
outChannel.close();
}
} catch (IOException e) {
log.error("파일 전송 실패", e);
}
log.error("파일 전송 실패", e);
}

byteBuffer.position(0); //파일을 저장하면서 position값이 변경되었으므로 0으로 초기화한다.
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/market/server/utils/DateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.market.server.utils;

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

public class DateUtil {

/**
* 파일 처리 시간을 yyyyMMddHHmm 형태로 반환 한다.
*
* @param fileType 파일 타입 jpg, png
* @return 처리 시간의 문자열 202009040159.jpg
* @author topojs8
*/
public static String getNowTimeToyyyyMMddHHmm(Date date, String fileType) {
return new SimpleDateFormat("yyyyMMddHHmm").format(date) + fileType;
}

/**
* 파일 처리 문자열 yyyyMMddHHmm 형태 인지 확인 한다.
*
* @param date 처리 시간 문자열
* @return void
* @author topojs8
*/
private static void parseDate(String dateStr) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date date = simpleDateFormat.parse(dateStr);
System.out.println("Successfully Parsed Date " + date);
} catch (ParseException e) {
System.out.println("ParseError " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

}

0 comments on commit 5fb8d10

Please sign in to comment.