1. 가장 직접적이고 간단한 방법은 파일 주소를 html 페이지의 링크에 직접 넣는 것입니다. 단점은 서버상의 파일 경로가 노출되어 파일 다운로드에 대한 다른 제어(권한 등)를 수행할 수 없다는 점입니다. 여기에는 예를 쓰지 않겠습니다.
2. 서버 측에서 파일은 출력 스트림으로 변환되어 응답에 기록되고 파일은 응답과 함께 브라우저로 가져옵니다. 브라우저는 사용자에게 파일을 로컬에 저장할 것인지 묻는 메시지를 표시합니다. (아래 예)
<%
response.setContentType(fileminitype);
response.setHeader("위치",파일명);
response.setHeader("Cache-Control", "max-age=" + 캐시타임);
response.setHeader("Content-Disposition", "attachment; filename=" + filename); //파일 이름은 인코딩되어야 합니다(utf-8).
response.setContentLength(파일 길이);
OutputStream 출력스트림 = response.getOutputStream();
InputStream inputStream = new FileInputStream(파일 경로);
바이트[] 버퍼 = 새 바이트[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(버퍼, 0, i);
}
출력스트림.플러시();
출력스트림.닫기();
inputStream.close();
출력스트림 = null
%>
3. JSP이므로 Applet을 사용하여 파일을 다운로드하는 방법도 있습니다. 그러나 고객은 먼저 서블릿에서 보낸 데이터 스트림을 받아들이고 로컬로 쓰는 애플릿 애플릿을 신뢰해야 합니다.
서블릿 측 예
공개 무효 서비스(HttpServletRequest req, HttpServletResponse res)
ServletException, IOException이 발생합니다.
res.setContentType(" text/plain ");
OutputStream 출력스트림 = null;
노력하다 {
outputStream = res.getOutputStream();
popFile(srcFile,outputStream));//파일 경로가 srcFile인 파일을 출력스트림에 씁니다.
} 잡기(IOException e) {
e.printStackTrace();
}
}
JApplet 측 예시
URLConnection con;
노력하다 {
con = url.openConnection();//url은 http://localhost:8080/sendDateSevlet.do 와 같은 호출된 SERVLET의 URL입니다.
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("콘텐츠 유형",
"응용 프로그램/옥텟 스트림");
InputStream in = con.getInputStream();
ProgressMonitorInputStream pmInputStream = 새로운 ProgressMonitorInputStream(
창, "서버에서 파일 콘텐츠 다운로드", in);
ProgressMonitor pMonitor = pmInputStream
.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(3);
pMonitor.setMillisToPopup(3);
String localfilepath = localstr + filename;//localfilepath 로컬 경로, localstr 파일 폴더, 파일 이름 로컬 파일 이름
if(saveFilsaveFilee(localfilepath,pmInputStream)){ //saveFilsaveFilee 메소드는 입력 스트림 pmInputStream을 파일 localfilepath에 씁니다.
openLocalFile(localfilepath);
}
4. 그런데 JApplet에서 파일을 업로드하는 코드도 게시되어 있습니다.
JApplet 측 예제
URLConnection con;
노력하다 {
con = url.openConnection();//url은 http://localhost:8080/sendDateSevlet.do 와 같은 호출된 SERVLET의 URL입니다.
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("콘텐츠 유형",
"응용 프로그램/옥텟 스트림");
OutputStream 출력 = con.getOutputStream();
String localfilepath = localstr + filename; //localfilepath 로컬 경로, localstr 파일 폴더, filename 로컬 파일 이름
getOutputStream(localfilepath,out);//getOutputStream 파일은 출력 스트림 out에 localfilepath 파일을 씁니다.
InputStream in = con.getInputStream();
사실을 반환;
}catch(IOException e) {
System.out.println("파일 업로드 오류!");
e.printStackTrace();
}
서블릿 측 코드 예
공개 무효 서비스(HttpServletRequest req, HttpServletResponse res)
ServletException, IOException이 발생합니다.
res.setContentType(" text/plain ");
입력스트림 입력스트림 = null;
노력하다 {
inputStream = res.getInputStream();
writefile(srcFile, inputStream);//입력 스트림 inputStream을 파일 경로가 srcFile인 파일에 저장합니다.
} 잡기(IOException e) {
e.printStackTrace();
}
} // 서비스 종료
요약: 파일 전송에서는 스트림 형태로 존재하며, 하드디스크에서는 파일 형태로 존재한다. 우리가 해야 할 일은 HttpServletRequest와 HttpServletResponse, 또는 응답과 요청을 통해 스트림을 보내고 스트림을 읽는 것뿐입니다. 파일을 스트림으로 변환하거나 스트림을 파일로 변환하는 작업도 마찬가지입니다.