JSPSMARTupload upload download full Raiders

xiaoxiao2021-03-06  37

1. Installing JSPSMARTupload is a free-use full-featured file that is developed by www.jspsmart.com, which is adapted to embed the JSP file that performs upload download operations. This component has the following features: 1, easy to use. Only writing three five lines of Java code in the JSP file, you can get upload or download it, it is convenient. 2, can control upload. With the objects provided by the JSPSmartupLoad component, you can get information on all uploaded files (including file name, size, type, extension, file data, etc.) for easy access. 3, can limit the uploaded documents in size, type and other aspects. This can be filtered off the files that do not meet the requirements. 4, flexible download. Only write two lines of code, you can turn the web server into a file server. No matter whether the file is in the directory of the web server or in any other directory, JSPSMARTupLoad can be downloaded. 5. Upload the file to the database, you can also download the data in the database. This function is for the mysql database because it is not versatile, so this article is not prepared to introduce this usage. The JSPSmartupLoad component can freely download from www.jspsmart.com, the name of the compressed package is jspsmartupload.zip. After downloading, use Winzip or WinRAR to extract it to Tomcat's WebApps directory (this article is described as an example of Tomcat Server). After decompression, change the subdirectory Web-INF in the WebApps / Jspsmartupload directory to all-in-case WEB-INF, so you can change the JSPSMARTupLoad class. Because Tomcat is sensitive to file name sensitive, it requires web applications related to the directory to Web-INF, and must be uppercase. Then restart Tomcat so that you can use the JSPSmartupload component in the JSP file. Note that after the above method is installed, only the program in the WebApps / JSPSMARTupload directory can use the JSPSMARTUPLOAD component, if you want the Tomcat server to use it, you must do the following work: 1. Enter the command line status, switch the directory to the Tomcat's WebApps / JSPSMARTUPLOAD / Web-INF directory. 2. Run JAR packaging command: JAR CVF JSPSMARTUPLOAD.JAR COM (You can also open the Explorer, switch to the current directory, compress all the files in the COM directory into jspsmartupload.zip with Winzip, then rename JSPSMARTUPLOAD.JAR Document can be. 3. Copy JSPSmartupload.jar to Tomcat's Shared / Lib Directory. Second, Related Class Descriptions (1) File class packages all information for uploading files. Through it, you can get the file name, file size, extension, file data, etc. of the upload file. The FILE class mainly provides the following methods: 1. SaveAs effect: Save the file.

Prototype: public void saveAs (java.lang.String destFilePathName) or public void saveAs (java.lang.String destFilePathName, int optionSaveAs) which, destFilePathName is saved in the file name, optionSaveAs is the Save option, this option has three values, respectively Is Saveas_Physical, SaveAs_Virtual, Saveas_Auto. SaveAs_physical indicates that the root directory of the operating system is saved as the file root directory, and saveas_virtual indicates that the root directory is saved by the root directory of the web application, and SaveAs_Auto means that the component is determined, and when the root directory of the web application exists When it chooses SaveAs_virtual, otherwise SaveAs_physical will be selected. For example, if the SaveAs ("/ UPLOAD / SAMPLE.ZIP", SaveAs_Physical) If the web server is installed in the C drive, the saved file name is actually C: /upload/sample.zip. And SaveAs ("/ upload / sample.zip", saveas_virtual) If the root directory of the web application is WebApps / JSPSmartupload, the saved file name is actually WebApps / JSPSmartupload / Upload / Sample.zip. SaveAs ("/ UPLOAD / SAMPLE.ZIP", SAVEAS_AUTO) If there is an UPLOAD directory in the web application root directory, its effect is the same as saveas ("/ upload / sample.zip", saveas_virtual), otherwise Saves ("/ UPLOAD / SAMPLE.ZIP ", SaveAs_physical). Recommendation: For the development of web programs, it is best to use SaveAs_virtual to transplant. 2, ismissing effect: This method is used to determine if the user selects the file, that is, the corresponding form item is worthless. When the file is selected, it returns false. When the file is not selected, it returns True. Prototype: public boolean ismissing () 3, getfieldname effect: Take the name of the form item corresponding to this uploaded file in the HTML form. Prototype: public string getfieldname () 4, getFileName effect: Take a file name (excluding directory information) prototype: public string getFileName () 5, getFilepathname effect: Take a full name (with directory) prototype: public string getFilePathname 6, getFileExt : Take a file extension (suffix) prototype: public string getfileext () 7, getSize effect: Take a file length (in bytes) prototype: public int gettsize () 8, getBinaryData effect: Take one of the file data indicated in the file data Bytes, used to detect files, etc. Prototype: Public Byte GetBinaryData (int index). Among them, Index represents the displacement, its value is between 0 and GetSize () - 1.

(Ii) Files class This class represents the collection of all uploaded files, which can get the number, size and other information of the upload file. There are the following methods: 1. GetCount role: Number of uploaded documents. Prototype: Public int getCount () 2, getFile effect: obtain the file object file in the specified displacement (this is com.jspsmart.upload.file, not java.io.file, pay attention to distinguish). Prototype: Public File GetFile (int index). Where index is a pointing, its value is between 0 and GetCount () - 1. 3, GetSize effect: Take the total length of the upload file, which can be used to limit the amount of data amounts uploaded. Prototype: Public long getSize () 4, getCollection effect: Return all uploaded file objects in the form of Collection so that other application references, browse upload file information. Prototype: Public Collection getCollection () 5, GetEnumeration effect: Return all upload file objects in ENUMERATION (enumeration) so that other applications browse uploaded file information. Prototype: Public Enumeration GetENUMERATION () ㈢ Request class This class is equivalent to the built-in object Request with the JSP. Only, this class is provided because the value of the form item cannot be obtained through the Request object for the file upload form. It must be obtained by the Request object provided by the JSPSmartupLoad component. This class provides the following methods: 1. GetParameter effect: Get the value of the specified parameter. When the parameter does not exist, the return value is NULL. Prototype: Public String getParameter (String name). Where Name is the name of the parameter. 2, GetParameterValues ​​effect: When a parameter can have multiple values, use this method to take the value. It returns a string array. When the parameter does not exist, the return value is NULL. Prototype: public string [] getParameterValues. Where Name is the name of the parameter. 3. GetParameterNames effect: obtain the name of all parameters in the Request object, used to traverse all parameters. It returns an enumerated object. Prototype: public enumeration getParameterNames () ㈣ Smartupload class This class completes upload download work. A. Method for uploading and downloading shared: there is only one: Initialize. Role: Execute the initialization work of upload download, must be the first execution. Prototype: There are multiple, mainly using this: public final void initialize (javax.servlet.jsp.pagecontext pagecontext) where PageContext is a JSP page built-in object (page context). B. Method for Uploading files: 1, Upload effect: Upload file data. For upload operations, the first step is executed, and the second step is to perform this method.

Prototype: Public Void Upload () 2, SAVE Role: Save all upload files to the specified directory and return the number of files saved. Prototype: Public int SAVE (String destPathname) and public int save (string destpathname, int option) where destPathname is a file saved directory, Option is saved, which has three values, Save_Physical, Save_Virtual and Save_Auto. (Similar to the value of the SaveAs method of the FILE class) Save_physical indicates that the component saves the file to the directory of the operating system root to the root directory, save_virtual indicates that the file saves the file to the web application root directory as the file root directory. In the directory, Save_AUTO indicates automatic selection by the component. Note: The effect of Save is equivalent to Save (Destpathname, Save_AUTO). 3, GetSize effect: Take the total length prototype of the upload file data: public int getSize () 4, getFiles effect: Take all uploaded files, return to the Files object, you can use the Files class to get the number of uploaded files, etc. . Prototype: Public Files getFiles () 5, getRequest effect: obtain the Request object so that this object obtains the value of the upload form single parameter. Prototype: Public Request getRequest () 6, SetallowedFileSlist effect: Settings Allow upload with files with specified extensions, the components will throw an exception when there is a file name in the upload. Prototype: Public Void SetAllowedFileSlist (String ALLOWEDFILSL) where allowedFileSlist is a comma-separated list of file extensions to allow uploaded file extensions. If you want to allow files that do not have extensions, you can use two comma. For example: SETALLOWEDFILSLIST ("DOC, TXT,") will allow files with DOC and TXT extensions and files without extensions. 7, SetDeniedFileSlist effect: Used to address those files with specified extensions. If there is a file extension being restricted, the upload component will throw an exception. Prototype: Public void setdeniedFilesList (String DeniedFilesList) where DeniedFileSlist is disabled from the uploaded file extension list, each extension is separated by commas. If you want to prohibit uploading files that do not have extensions, you can use two commas to represent. For example: SetDeniedFileSlist ("EXE, BAT,") will disable files with EXE and BAT extensions and files without extensions. 8, SetMaxFileSize effect: Set the maximum length of each file to be uploaded. Prototype: Public Void SetMaxFileSize (long maxfilesize) where the maxFileSize allows uploading the maximum length for each file. When the file exceeds this length, it will not be uploaded. 9, SetTotalMaxFileSize effect: Set the total length of the file that allows uploaded files to limit the amount of data of a disposable upload.

Prototype: Public void setTotalmaxFileSize (long TotalmaxFileSize) where TotalMaxFileSize is the total length of the file that allows uploaded files. C. Download files commonly used method 1, setContentDisPosition effect: Add data to the Content-Disposition Domain of the MIME file header. The JSPSMARTupLoad component automatically fills in the Content-Disposition Domain of the MIME File Header when the download is returned. If the user needs to add additional information, use this method. Prototype: Public void setContentDisPosition (String ContentDisPosition) where contentdisposition is the data to be added. If contentdisposition is null, the component will automatically add "attachment;" to indicate that the downloaded file as an attachment, the result is that the IE browser will prompt the file instead of automatically opening this file (IE browser usually according to the downloaded file The extension decides what operations are executed, the extension is that the DOC will open with the Word program, and the extension will open with the Acrobat program, and so on). 2, Downloadfile: Download file. Prototype: A total of three prototypes available, the first most common, the latter two file downloads for special circumstances (such as changing the content type, change the saved file name). ① public void downloadFile (String sourceFilePathName) which, sourceFilePathName want to download the file name (a file with a directory full name) ② public void downloadFile (String sourceFilePathName, String contentType) which, sourceFilePathName want to download the file name (with a directory of files Full name), ContentType is the content type (file type information in MIME format, can be identified by browser). 3 Public Void Downloadfile (String SourceFilepathname, String ContentType, String DestfileName) where SourceFilePathname is the file name (with directory file full name), contentType is the content type (file type information in MIME format, can be identified by browser) DestfileName is the default saved file name for download. Third, the file upload (1) Form form for uploading files, two requirements: 1. Method Apply POST, Method = "POST".

2, increase the attribute: encType = "multipart / form-data" Below is an example of a Form form for uploading files:

(ii) Uploaded example 1, upload page Upload.html This page provides a form to allow users to select to upload The file, click the "Upload" button to perform the upload operation.

The page source code is as follows: 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" encty = "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> <div align = "center"> 2, <input type = "File" name = "file2" size = "30"> </ div> <td> <div align = "center"> 3, <input type = "file" Name = "file3" size = "30"> <tr> <td> <div align = "center"> 4, <input type = "file" name = "file4 "SIZE =" 30 "> </ div> <TD> </ TD> <div align =" center "> <input type =" Submit "Name =" Submit "Value =" uploaded it! "</p> <p>> </ Div> </ td> </ tr> </ table> </ form> </ body> </ html> 2, upload processing page DO_UPLOAD.JSP This page performs file upload operations. The usage of the upload method is described in detail in the page source code, and details will not be described here. The page source code is as follows: <% - File name: do_upload.jsp author: vertical and horizontal software production center rain is also aqi (zhsoft88@sohu.com) -%> <% @ 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> <% // New a smartupload object Smartupload Su = new smartupload (); // Upload initialization Su.initialize (PageContext ); // Set up the upload limit // 1. Restrict the maximum length of each upload file. // Su.setMaxFileSize (10000); // 2. Restrict the length of the total upload data. // Su.SettotalMaxFileSize (20000); // 3. Set the file that allows upload (by extension restrictions), only the DOC, TXT file is allowed. // Su.setAllowedFileSlist ("DOC, TXT"); // 4. Set the file that prohibits uploading (by extension restrictions), prohibiting uploading files with EXE, BAT, JSP, HTM, HTML extensions and no expansion Name file. // Su.SetdeniedFileSlist ("EXE, BAT, JSP, HTM, HTML,"); // Upload file Su.upload (); // Save all of the upload files to the specified directory int count = Su.Save ("/ UPLOAD "); Out.println (Count " file upload success! <br> "); // Use the Request object to obtain parameters of parameters Out.println (" test = " su.getRequest (). getParameter (" test " ) "<BR> <BR>"); // upload files one by one to extract information, and save the file.</p> <p>For (int i = 0; i <su.getfiles (). getCount (); i ) {com.jspsmart.upload.file file = su.getfiles (). getFile (i); // If the file does not exist, continue IF (file.isming ()) Continue; // Displays the current file information out.println ("<Table Border = 1>"); out.println ("<tr> <TD> Fieldname </ TD) > <TD> " file.getfieldname () " </ td> </ tr> "); out.println (" <tr> <td> file length (size) </ td> <TD> File .getsize () "</ td> </ tr>"); out.println ("<tr> <td> file name (filename </ td> <td>" file.getFileName () < / TD> </ TR> "); OUT.PRINTLN (" <tr> <td> file extension (fileext) </ td> <TD> " file.getfileext () " </ td> </ tr > "); Out.println (" <tr> <td> file full name (filepathname </ td> <td> file.getfilepathname () "</ td> </ tr>"); OUT. Println ("</ Table> <br>"); // Save the file // file.saves ("/ upload /" myfile.getFileName ()); // Save to the root directory as the web application as a file The root directory of the root directory // file.saves ("/ upload /" myfile.getFileName (), su.save_virtual; / / Save to the root directory of the operating system to the file root directory // file.saveas "c: // Temp //" myfile.getFileName (), su.save_physical;}%> </ body> </ html> four, file download artile 1, download link page Download.html page source code as follows: < ! - File Name: Downlo Ad.html author: Crossing software production center rain is also true (zhsoft88@sohu.com) -> <! doctype html public "- // w3c // DTD HTML 4.01 Transitional // En"> <html> <head> <title> Download </ Title> <meta http-equiv = "Content-Type" Content = "</p> <p>Text / html; charset = GB2312 "> </ head> <body> <a href="jsp/do_download.jsp"> Click to download </a> </ body> </ html> 2, download processing page do_download.jsp Do_download.jsp demonstrates how to download files using JSPSmartupload components, you can see from the source code below, and how simple downloads are downloaded. The source code is as follows: <% @ Page ContentType = "TEXT / HTML; Charset = GB2312" Import = "COM. JSPSmart.upload. * "%> <% // Newly built a smartupload su = new smartupload (); // Initialize Su.initialize (PageContext); // Set contentdisposition to null to prohibit the browser automatically open files, / / Ensure that the click link is downloaded after the link. If the downloaded file extension is // DOC, the browser will automatically open it with Word. When the extension is PDF, // The browser will open with Acrobat. Su.SetContentDisPosition (NULL); // Download file Su.downloadfile ("/ UPLOAD / How to earn my first bucket gold.doc");%> Note, perform downloaded pages, outside the Java script (ie < % ...%>, do not include characters such as HTML code, space, carriage return, and wrap, and some will not download correctly. If you don't believe, you can add a newline between%> <% in the above source code. , Download it again, ensure error. Because it affects the data stream that returns to the browser, result in a resolution error. , When the browser is prompted, it shows a bunch of garbled, very sweeping. The example is the case. (This problem is also the problem that there is many downloads, few people solve, search Relevant information, sigh!) In order to add support for the JSPSmartupLoad component, I have studied the component and found that the browser can correctly display Chinese after UTF-8 encoding for the saving file name returned to the browser. The name is. This is a happy discovery.</p> <p>So I made of SmartUpload class jspSmartUpload components of the upgrade process, increase toUtf8String this method, alter some source code as follows: 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 IllegalGumentException (" 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-msdown"); Else IF (S1.Length () == 0) m_response.setContentType ("Application / X-MSDownload") Else M_Response.setContentType (S1); M_RESPONSE.SETCONTENTLENGTH ((int) L); M_Contentdisposi !? Tion = 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 =</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-70003.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="70003" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.044</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'GQI2s8DpB4KX8Vm4Igu2PO3pIu3aosY86udoJMG0d2iiNSy1syk9oB5PW0In83rejAK_2FEMIoFTFFxocUnQcvsw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>