티스토리 뷰
InputStream in = null;
OutputStream out = null;
File file = null;
try{
file = new File(path, newName);
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
oldName = URLEncoder.encode(oldName, "UTF-8").replaceAll("\\+", " ");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + oldName);
byte[] b = new byte[(int)file.length()];
int leng = 0;
while((leng = in.read(b)) > 0){
out.write(b, 0, leng);
}
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
업로드에 비해 비교적 쉽다.
하나 첨언할것은 .replace("\\+", " ") 이부분인데 파일명에 공백이 있을경우 그 공백을 +로 출력하기때문에 그 +를 다시 공백으로 바꿔주는 작업임.
'Java' 카테고리의 다른 글
현재시간구하기 (0) | 2015.06.06 |
---|---|
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path (27) | 2015.01.24 |
파일업로드 (0) | 2014.12.23 |
java Socket 관련 오류 (0) | 2014.12.22 |
[Exception] [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause (0) | 2014.12.19 |
- Total
- Today
- Yesterday
- OOP
- 정규표현식
- backend개발환경
- DesignPattern
- go-core
- generics
- Git
- javascript
- programming
- Spring
- TEST
- spring cloud
- mariadb
- frontcode
- clean code
- EffectiveJava
- Kotlin
- JPA
- Jackson
- toby
- frontend개발환경
- java
- java8
- MySQL
- code
- servlet
- JavaScript Core
- Design Pattern
- db
- http
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |