Repository navigation
Expand file tree
/
Copy pathStudyDownloadCommand.java
More file actions
59 lines (44 loc) · 1.69 KB
/
Copy pathStudyDownloadCommand.java
File metadata and controls
59 lines (44 loc) · 1.69 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.Study;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Post.postDAO;
import com.Post.postDTO;
public class StudyDownloadCommand implements StudyCommand{
@Override
public int execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
int postId = Integer.parseInt(request.getParameter("postId"));
postDAO dao = postDAO.getInstance();
postDTO dto = dao.viewPost(postId);
String fileName = dto.getFileUrl();
ServletContext context = request.getSession().getServletContext();
String downaloadPath = context.getRealPath("upload");
String filePath = downaloadPath + "/" + fileName;
File file = new File(filePath);
System.out.println("경로는 : " + filePath);
String mimeType = context.getMimeType(filePath);
if(mimeType == null)
mimeType = "application/octet-stream";
response.setContentType(mimeType);
String encoding = new String(fileName.getBytes("utf-8"), "8859_1");
response.setHeader("Content-Disposition", "attachment; filename=" + encoding);
response.setContentType("text/html; charset=utf-8");
response.setHeader("Chche-Control", "no-cache");
FileInputStream in = new FileInputStream(file);
ServletOutputStream outStream = response.getOutputStream();
byte b[] = new byte[4096];
int data = 0;
while ((data = in.read(b, 0, b.length)) != -1) {
outStream.write(b, 0, data);
}
outStream.flush();
outStream.close();
in.close();
return postId;
}
}