인프런 커뮤니티 질문&답변
파일전송 할 때, 메세지 바디 출력
작성
·
179
0
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container">
<div class="py-5 text-center">
<h2>상품 등록 폼</h2>
</div>
<h4 class="mb-3">상품 입력</h4>
<form th:action method="post" enctype="multipart/form-data">
<ul>
<li>상품명 <input type="text" name="itemName"></li>
<li>파일<input type="file" name="file" ></li>
</ul>
<input type="submit"/>
</form>
</div> <!-- /container -->
</body>
</html>
@PostMapping("/add")
public String saveFileV1(HttpServletRequest request) {
try {
ServletInputStream inputStream = null;
inputStream = request.getInputStream();
System.out.println(inputStream.read());
byte[] bytes = inputStream.readAllBytes();
for (byte aByte : bytes) {
System.out.print(aByte);
}
} catch (IOException e) {
e.printStackTrace();
}
return "page";
}
안녕하세요. 파일전송 할 때, 메세지 바디 출력하려고 하는데 빈문자열이 출력되어서 질문드립니다.
몇가지 단서를 알려드리면
1. request.getParamter("itemName") 하면 올바르게 출력 됩니다.
2. request.getContentType() 하면 multipart/data-form 잘 출력 됩니다.
3. inputStream.read() 해서 읽으려하면 -1 출력됩니다.(메세지 바디에 전송된게 없다는 거겠죠?..; 근데 파라미터로는 읽었다니요)
몇시간 째 풀리지 않고 있는데 도와주세요 ㅠㅠㅠ
답변 1
0
김영한
지식공유자
안녕하세요. 우디님
이 부분은 저도 정확히는 잘 모르겠습니다.
참고로 스트림은 어디선가 한번 읽게 되면 다시 읽는 것이 불가능합니다.
파일의 경우 이미 스트림을 어디선가 읽어 버렸을 가능성이 있습니다.
관련해서 자세히 아시는 분 있으면 답변 남겨주세요.
감사합니다.





