<FORM METHOD="POST" ENCTYPE="다중 부분/양식-데이터" ACTION="/jspSmartUpload/upload.jsp"> <입력 유형="파일" 이름="내 파일"> <입력 유형="제출"> </FORM> |
<!--
파일 이름: upload.html
작성자: Zongheng Software Production Center의 Yu Yiqi([email protected])
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<머리>
<title>파일 업로드</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<본문>
<p> </p>
<p align="center">파일 선택 업로드</p>
<FORM METHOD="POST" ACTION="jsp/do_upload.jsp"
ENCTYPE="다중 부분/양식-데이터">
<input type="hidden" name="TEST" value="good">
<테이블 너비="75%" border="1" align="center">
<tr>
<td><div align="center">1.
<입력 유형="FILE" 이름="FILE1" 크기="30">
</div></td>
</tr>
<tr>
<td><div align="center">2.
<입력 유형="FILE" 이름="FILE2" 크기="30">
</div></td>
</tr>
<tr>
<td><div align="center">3.
<입력 유형="FILE" 이름="FILE3" 크기="30">
</div></td>
</tr>
<tr>
<td><div align="center">4.
<입력 유형="FILE" 이름="FILE4" 크기="30">
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="업로드하세요!">
</div></td>
</tr>
</table>
</FORM>
</body>
</html> |
<%-- 파일 이름: do_upload.jsp 작성자: Zongheng Software Production Center의 Yu Yiqi([email protected]) --%> <%@ 페이지 contentType="text/html; charset=gb2312" 언어="java" import="java.util.*,com.jspsmart.upload.*" errorPage="" %> <html> <머리> <title>파일 업로드 처리 페이지</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <본문> <% // 새 SmartUpload 개체 만들기 SmartUpload su = new SmartUpload(); //초기화 업로드 su.initialize(pageContext); // 업로드 제한 설정 // 1. 업로드된 각 파일의 최대 길이를 제한합니다. // su.setMaxFileSize(10000); // 2. 업로드되는 전체 데이터의 길이를 제한합니다. // su.setTotalMaxFileSize(20000); // 3. 업로드 허용(확장자 제한) 파일을 설정합니다. doc, txt 파일만 허용됩니다. // su.setAllowedFilesList("doc,txt"); // 4. 업로드 금지 파일(확장자 제한)을 설정하고, exe, bat, jsp, htm, html 확장자가 있는 파일과 확장자가 없는 파일. // su.setDeniedFilesList("exe,bat,jsp,htm,html,,"); //파일 업로드 su.upload(); // 업로드된 모든 파일을 지정된 디렉터리에 저장합니다. int count = su.save("/upload"); out.println(count+"파일이 성공적으로 업로드되었습니다!<br>"); //Request 객체를 사용하여 매개변수 값을 얻습니다. out.println("TEST="+su.getRequest().getParameter("TEST") +"<BR><BR>"); // 업로드된 파일 정보를 하나씩 추출하여 동시에 파일을 저장합니다. for (int i=0;i<su.getFiles().getCount();i++) { com.jspsmart.upload.File 파일 = su.getFiles().getFile(i); // 파일이 없으면 계속합니다. if (file.isMissing()) continue; // 현재 파일 정보를 표시합니다. out.println("<TABLE BORDER=1>"); out.println("<TR><TD>양식 이름(FieldName)</TD><TD>" + file.getFieldName() + "</TD></TR>"); out.println("<TR><TD>파일 길이(크기)</TD><TD>" + file.getSize() + "</TD></TR>"); out.println("<TR><TD>파일 이름(파일 이름)</TD><TD>" + file.getFileName() + "</TD></TR>"); out.println("<TR><TD>파일 확장자(FileExt)</TD><TD>" + file.getFileExt() + "</TD></TR>"); out.println("<TR><TD>파일 전체 이름(FilePathName)</TD><TD>" + file.getFilePathName() + "</TD></TR>"); out.println("</TABLE><BR>"); // 파일을 // file.saveAs("/upload/" + myFile.getFileName())로 저장합니다. // WEB 애플리케이션의 루트 디렉터리를 파일 루트 디렉터리로 사용하여 디렉터리에 저장합니다. // file.saveAs("/upload/" + myFile.getFileName(), su.SAVE_VIRTUAL); // 운영체제의 루트 디렉터리가 파일 루트 디렉터리인 디렉터리에 저장 // file.saveAs("c:\temp\" + myFile.getFileName(), su.SAVE_PHYSICAL); } %> </body> </html> |
<!-- 파일 이름: download.html 작성자: Zongheng Software Production Center의 Yu Yiqi([email protected]) --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <머리> <title>다운로드</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <본문> <a href="jsp/do_download.jsp">다운로드하려면 클릭하세요</a> </body> </html> |
<%@ 페이지 contentType="text/html;charset=gb2312"
import="com.jspsmart.upload.*" %><%
// 새 SmartUpload 개체 만들기 SmartUpload su = new SmartUpload();
//초기화 su.initialize(pageContext);
//브라우저가 자동으로 파일을 열지 못하도록 하려면 contentDisposition을 null로 설정합니다.
//링크를 클릭한 후 파일이 다운로드되었는지 확인합니다. 설정하지 않으면 다운로드한 파일의 확장자가 //doc일 때 브라우저가 자동으로 단어로 열립니다. 확장자가 pdf인 경우,
//Acrobat으로 브라우저가 열립니다.
su.setContentDisposition(null);
// 파일 다운로드 su.downloadFile("/upload/gold.doc의 첫 번째 냄비를 얻는 방법");
%> |
공개 무효 다운로드 파일(문자열 s, 문자열 s1, 문자열 s2, int i)
ServletException, IOException, SmartUploadException이 발생합니다.
{
if(s==null)
새로운 IllegalArgumentException("파일 '" + s +
"' 찾을 수 없습니다(1040).");
if(s.equals(""))
새로운 IllegalArgumentException("파일 '" + s +
"' 찾을 수 없습니다(1040).");
if(!isVirtual(s) && m_denyPhysicalPath)
throw new SecurityException("물리적 경로는
거부됨(1035).");
if(isVirtual(들))
s = m_application.getRealPath(s);
java.io.File 파일 = 새로운 java.io.File(s);
FileInputStream fileinputstream = new FileInputStream(파일);
긴 l = file.length();
부울 플래그 = false;
정수 k = 0;
바이트 abyte0[] = 새 바이트[i];
if(s1==널)
m_response.setContentType("application/x-msdownload");
또 다른
if(s1.length() == 0)
m_response.setContentType("application/x-msdownload");
또 다른
m_response.setContentType(s1);
m_response.setContentLength((int)l);
m_contentDisposition = m_contentDisposition != null ?
m_contentDisposition: "첨부파일;";
if(s2 == null)
m_response.setHeader("콘텐츠 처리",
m_contentDisposition + " 파일 이름=" +
toUtf8String(getFileName(s)));
또 다른
if(s2.length() == 0)
m_response.setHeader("콘텐츠 처리",
m_contentDisposition);
또 다른
m_response.setHeader("콘텐츠 처리",
m_contentDisposition + " filename=" + toUtf8String(s2));
동안((긴)k < l)
{
int j = fileinputstream.read(abyte0, 0, i);
k += j;
m_response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
}
/**
* 다운로드 시 저장된 파일명이 올바르게 표시될 수 있도록 파일명 중 한자를 UTF8 인코딩 문자열로 변환하세요.
* Zongheng 소프트웨어 생산 센터 Yu Yiqi 2003.08.01
* @param s 원본 파일 이름* @return 다시 인코딩된 파일 이름*/
공개 정적 문자열 toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} 또 다른 {
바이트[]b;
노력하다 {
b = Character.toString(c).getBytes("utf-8");
} catch (예외예외) {
System.out.println(ex);
b = 새 바이트[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).
toUpperCase());
}
}
}
sb.toString()을 반환합니다.
} |