Smartupload implementing file Uploads

Source: Internet
Author: User
Tags save file

(a) Introduction to Smartupload components

Smartupload Componentsfree components for file uploads and downloads (ii) features of Smartupload componentsSimple to use: Write a small amount of code, complete the upload download functionability to control upload contentability to control the size and type of uploaded filesCons: The update service is currently stopped

(iii) SMARTUPLOAD component application conditions

File classes and methods

Smartupload class and method

Implementation of File Upload

Control the type of upload file

Control the size of uploaded files

Attention:

㈠ form Requirements

For the form form that uploads the file, there are two requirements:

1, Method application post, that is, method= "post".

2. Add attribute: enctype= "Multipart/form-data"

Case: Here is an example of the form form for uploading a file:

form page indes.jsp<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <Basehref= "<%=basePath%>">        <title>Smartupload</title>      </Head>    <Body>    <formAction= "doupload.jsp"Method= "POST"enctype= "Multipart/form-data">Name:<inputtype= "text"name= "username" /><BR/>Password:<inputtype= "text"name= "pwd" /><BR/>file 1:<inputtype= "File"name= "File1" /><BR/>File 2:<inputtype= "File"name= "File2" /><BR/>File 3:<inputtype= "File"name= "File3" /><BR/>File 4:<inputtype= "File"name= "File4" /><BR/>file 5:<inputtype= "File"name= "File5" /><BR/>File 6:<inputtype= "File"name= "File6" /><BR/>File 7:<inputtype= "File"name= "File7" /><BR/>        <inputtype= "Submit" />    </form>  </Body></HTML>

Working with Form pages

<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><% @pageImport= "Com.jspsmart.upload.SmartUpload"%><% @pageImport= "Com.jspsmart.upload.File"%><%//----processing upload files    /*use Smartupload 1, add component jar package to Web-inf/lib package 2, set the form form's commit type to binary type*/        //Creating Smartupload ComponentsSmartupload su =Newsmartupload (); //initializing the component contextsu.initialize (PageContext); //Set EncodingSu.setcharset ("Utf-8"); Try{                //allowed file types to upload//su.setallowedfileslist ("Doc"); //limit the types of uploaded files//su.setdeniedfileslist ("bat"); //limit the size of uploaded files//Su.setmaxfilesize (2000); //Uploading Filessu.upload (); }Catch(Exception e) {%> <script type= "Text/javascript" >Alert ("Do not allow uploads: Bat,exe,jsp,html,htm,java,class file Type"); Location.href= "Index.jsp"; </script> <%} out.println ("Upload" +su.save ("upload\\") + "successful!!! of Files"); //Get form ItemsString name = Su.getrequest (). GetParameter ("username"); String pwd= Su.getrequest (). GetParameter ("pwd"); /*implement multi-file uploads*/     for(inti = 0; I < Su.getfiles (). GetCount (); i++){                    //get a single fileFile File =su.getfiles (). GetFile (i); //determines whether the file is selected by the current document;        if(File.ismissing ())Continue; //set the location of the upload serverString Path = "Upload\\"; //get the name of the uploaded fileString filename =File.getfilename (); //the path to the uploaded filePath = path +filename; //Save FileFile.saveas (path,smartupload.save_virtual); Out.println ("<table border=1>"); Out.println ("<tr><td> name:</td><td>" +name+ "</td></tr>"); Out.println ("<tr><td> password:</td><td>" +pwd+ "</td></tr>"); Out.println ("<tr><td> uploaded file name (filename):</td><td>" +filename+ "</td></tr>"); Out.println ("<tr><td> uploaded file Table item name (fliedname):</td><td>" +file.getfieldname () + "</td></tr>"); Out.println ("<tr><td> uploaded file Size (length) (size):</td><td>" +file.getsize () + "</td></tr>"); Out.println ("<tr><td> uploaded file extension (ext):</td><td>" +file.getfileext () + "</td></tr>"); Out.println ("</table><br/>"); }%>

Smartupload implementing file uploads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.