JSP implementation file upload (ii) implement file upload with Cos component

Source: Internet
Author: User
Tags cos

JSP forms

<%@ page language= "java" pageencoding= "Utf-8"%>

Cos component implementation file upload JSP code

<%@ page language= "java" pageencoding= "utf-8"%><%@ pageImport= "Java.io.File"%><%@ pageImport= "Com.oreilly.servlet.multipart.FilePart"%><%@ pageImport= "Com.oreilly.servlet.multipart.ParamPart"%><%@ pageImport= "Com.oreilly.servlet.multipart.Part"%><%@ pageImport= "Com.oreilly.servlet.multipart.MultipartParser"%><%//using the Cos component to implement file uploadsMultipartparser MP =NewMultipartparser (Request, 10 * 1024 * 1024);    Part part; Mp.setencoding ("Utf-8");//depending on the source page encoding, if the position should be GBK, if the JSP should be Utf-8    intII = 0;  while(part = Mp.readnextpart ())! =NULL) {String name=Part.getname (); if(Part.isparam ()) {Parampart Parampart=(Parampart) part; String value=Parampart.getstringvalue (); Out.println ("Param:name=" + name + "; Value= "+value);        System.out.println (value); } Else if(Part.isfile ()) {Filepart Filepart=(Filepart) part; String FileName=Filepart.getfilename (); if(FileName! =NULL) {                LongSize = Filepart.writeto (NewFile ( This. Getservletcontext (). Getrealpath ("/upload"))); Out.println ("File:name=" + name + "; Filename= "+ FileName +", filepath= "+ filepart.getfilepath () +", contenttype= "+ fil Epart.getcontenttype () + ", size=" +size); }Else{out.println ("File:name=" + name + "; EMPTY ");        } out.print (FileName); }    }%>

File naming mechanism

1. According to the UUID

ImportJava.io.File;ImportJava.util.UUID;ImportCom.oreilly.servlet.multipart.FileRenamePolicy; Public classMyuuidImplementsFilerenamepolicy {@Override Publicfile Rename (file f) {String body=Uuid.randomuuid (). toString (); String ext= ""; intPot = F.getname (). LastIndexOf ("."); if(Pot! =-1) {ext=f.getname (). substring (pot); } String newName= Body +ext; F=NewFile (F.getparent (), newName); returnF; }}

2. According to the time random number

ImportJava.io.File;ImportJava.util.UUID;ImportCom.oreilly.servlet.multipart.FileRenamePolicy; Public classMyrandomImplementsFilerenamepolicy {@Override Publicfile Rename (file f) {String body=string.valueof (System.currenttimemillis ()); String ext= ""; intPot = F.getname (). LastIndexOf ("."); if(Pot! =-1) {ext=f.getname (). substring (pot); } String newName= Body +ext; F=NewFile (F.getparent (), newName); returnF; }}

Upload files and change names

<!DOCTYPE HTML><HTML>  <Head>    <MetaCharSet= "Utf-8">    <title>Cc.html</title>  </Head>    <Body>  <formAction= "cc.jsp"Method= "POST"enctype= "Multipart/form-data">Name:<inputtype= "text"name= "Uname"><BR>file 1:<inputtype= "File"name= "Ufile1"><BR>File 2:<inputtype= "File"name= "Ufile2"><BR> <inputtype= "Submit"value= "Submit">  </form>    </Body></HTML>

<% @pageImport= "com. Myrandom "%><%@ page language=" java "pageencoding=" utf-8 "%><%@ pageImport= "Com.oreilly.servlet.MultipartRequest"%><%@ pageImport= "Com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%><%@ pageImport= "com. Myuuid "%><%@ pageImport= "Java.io.File"%><%//using the Cos component to implement file uploadsString Path = This. Getservletcontext (). Getrealpath ("/upload"); //multipartrequest req = new Multipartrequest (Request, Path, "Utf-8"); //Out.print (Req.getparameter ("uname")); //out.print ("//Out.print (Req.getfile ("Ufile1"). GetName ()); //Out.print (Req.getfile ("Ufile2"). GetName ()); //multipartrequest req = new Multipartrequest (request,path,10*1024*1024, "Utf-8", New Myuuid ());Multipartrequest req =NewMultipartrequest (request,path,10*1024*1024, "Utf-8",Newmyrandom ()); Out.print (Req.getfile ("Ufile1"). GetName ()); %>

JSP implementation file upload (ii) implement file upload with Cos component

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.