<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="/jspSmartUpload/upload.jsp"> <INPUT TYPE="FILE" NAME="MYFILE"> <INPUT TYPE="SUBMIT"> </FORM> |
<!-- File name: upload.html Author: Yu Yiqi from Zongheng Software Production Center ([email protected]) --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>File upload</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <p> </p> <p align="center">Upload file selection</p> <FORM METHOD="POST" ACTION="jsp/do_upload.jsp" ENCTYPE="multipart/form-data"> <input type="hidden" name="TEST" value="good"> <table width="75%" border="1" align="center"> <tr> <td><div align="center">1. <input type="FILE" name="FILE1" size="30"> </div></td> </tr> <tr> <td><div align="center">2. <input type="FILE" name="FILE2" size="30"> </div></td> </tr> <tr> <td><div align="center">3. <input type="FILE" name="FILE3" size="30"> </div></td> </tr> <tr> <td><div align="center">4. <input type="FILE" name="FILE4" size="30"> </div></td> </tr> <tr> <td><div align="center"> <input type="submit" name="Submit" value="Upload it!"> </div></td> </tr> </table> </FORM> </body> </html> |
<%-- File name: do_upload.jsp Author: Yu Yiqi from Zongheng Software Production Center ([email protected]) --%> <%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" errorPage="" %> <html> <head> <title>File upload processing page</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <% // Create a new SmartUpload object SmartUpload su = new SmartUpload(); //Upload initialization su.initialize(pageContext); // Set upload limits // 1. Limit the maximum length of each uploaded file. // su.setMaxFileSize(10000); // 2. Limit the length of the total uploaded data. // su.setTotalMaxFileSize(20000); // 3. Set the files allowed to be uploaded (restricted by extension), only doc and txt files are allowed. // su.setAllowedFilesList("doc,txt"); // 4. Set files that are prohibited from being uploaded (restricted by extension), prohibit uploading files with exe, bat, Files with jsp, htm, html extensions and files without extensions. // su.setDeniedFilesList("exe,bat,jsp,htm,html,,"); //Upload files su.upload(); // Save all uploaded files to the specified directory int count = su.save("/upload"); out.println(count+"Files uploaded successfully!<br>"); //Use the Request object to obtain the value of the parameter out.println("TEST="+su.getRequest().getParameter("TEST") +"<BR><BR>"); // Extract uploaded file information one by one, and save files at the same time. for (int i=0;i<su.getFiles().getCount();i++) { com.jspsmart.upload.File file = su.getFiles().getFile(i); // Continue if the file does not exist if (file.isMissing()) continue; // Display current file information out.println("<TABLE BORDER=1>"); out.println("<TR><TD>Form name (FieldName)</TD><TD>" + file.getFieldName() + "</TD></TR>"); out.println("<TR><TD>File length (Size)</TD><TD>" + file.getSize() + "</TD></TR>"); out.println("<TR><TD>FileName (FileName)</TD><TD>" + file.getFileName() + "</TD></TR>"); out.println("<TR><TD>File Extension (FileExt)</TD><TD>" + file.getFileExt() + "</TD></TR>"); out.println("<TR><TD>Full name of file (FilePathName)</TD><TD>" + file.getFilePathName() + "</TD></TR>"); out.println("</TABLE><BR>"); // Save the file as // file.saveAs("/upload/" + myFile.getFileName()); // Save to a directory with the root directory of the WEB application as the file root directory // file.saveAs("/upload/" + myFile.getFileName(), su.SAVE_VIRTUAL); // Save to the directory where the root directory of the operating system is the file root directory // file.saveAs("c:\temp\" + myFile.getFileName(), su.SAVE_PHYSICAL); } %> </body> </html> |
<!-- File name: download.html Author: Yu Yiqi from Zongheng Software Production Center ([email protected]) --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Download</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <a href="jsp/do_download.jsp">Click to download</a> </body> </html> |
<%@ page contentType="text/html;charset=gb2312" import="com.jspsmart.upload.*" %><% // Create a new SmartUpload object SmartUpload su = new SmartUpload(); //Initialize su.initialize(pageContext); //Set contentDisposition to null to prevent the browser from automatically opening the file. // Ensure that the file is downloaded after clicking the link. If not set, when the extension of the downloaded file is //doc, the browser will automatically open it with word. When the extension is pdf, //The browser will be opened with acrobat. su.setContentDisposition(null); // Download file su.downloadFile("/upload/How to earn my first pot of gold.doc"); %> |
public void downloadFile(String s, String s1, String s2, int i) throws ServletException, IOException, SmartUploadException { if(s==null) throw new IllegalArgumentException("File '" + s + "' not found (1040)."); if(s.equals("")) throw new IllegalArgumentException("File '" + s + "' not found (1040)."); if(!isVirtual(s) && m_denyPhysicalPath) throw new SecurityException("Physical path is denied (1035)."); if(isVirtual(s)) s = m_application.getRealPath(s); java.io.File file = new java.io.File(s); FileInputStream fileinputstream = new FileInputStream(file); long l = file.length(); boolean flag = false; int k = 0; byte abyte0[] = new byte[i]; if(s1==null) m_response.setContentType("application/x-msdownload"); else if(s1.length() == 0) m_response.setContentType("application/x-msdownload"); else m_response.setContentType(s1); m_response.setContentLength((int)l); m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;"; if(s2 == null) m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + toUtf8String(getFileName(s))); else if(s2.length() == 0) m_response.setHeader("Content-Disposition", m_contentDisposition); else m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + toUtf8String(s2)); while((long)k < l) { int j = fileinputstream.read(abyte0, 0, i); k += j; m_response.getOutputStream().write(abyte0, 0, j); } fileinputstream.close(); } /** * Convert the Chinese characters in the file name to a UTF8 encoded string so that the saved file name can be correctly displayed when downloading. * Zongheng Software Production Center Yu Yiqi 2003.08.01 * @param s original file name* @return re-encoded file name*/ public static String 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); } else { byte[] b; try { b = Character.toString(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; } for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + Integer.toHexString(k). toUpperCase()); } } } return sb.toString(); } |