JSP upload file with cos and rename __js

Source: Internet
Author: User
Tags cos




1. Download the latest COS pack (http://www.servlets.com/cos/index.html) and add it to Web-inf/bin





2. Can upload multiple files.





3. The main use of the class





Part
Parampart
Filepart
Multipartparser



The UpLoad () code is as follows: Import Com.oreilly.servlet.multipart. * ;

Private Boolean upLoad (HttpServletRequest request, Map < string,string > Parammap)
Throws IOException ... {

/**//*
* Set upload maximum length
*/
int maxSize = 2 * 1024 * 1024;

/**//*
* Upload Path
*/
String Uploadpath = Getservletcontext (). Getrealpath ("/")
+ Constants.picuploadpath;

/**//*
* Upload part to determine whether it is a file or a property
* If it is a file, take the relevant action if it is a property, remove the attribute and put it in a map for processing
*/

Part part = NULL;
Multipartparser mrequest = new Multipartparser (request, maxSize);
Mrequest.setencoding ("GBK");
while (part = Mrequest.readnextpart ())!= null) ... {
if (Part.isfile ()) ... {

/**//*
* Converted to Filepart
*/
Filepart Filepart = (filepart) part;

String fileName = Filepart.getfilename ();

if (fileName!= null && filename.length () > 0) ... {
Get extension
String fileextname = filename.substring (
Filename.lastindexof (".") + 1). toLowerCase ();
Upload pictures only
if (fileextname.equals ("JPEG") | | fileextname.equals ("JPG")
|| Fileextname.equals ("gif")) ... {

String NewFileName = util.getnewfilename () + "."
+ Fileextname;
String NewPath = Uploadpath + newfilename;
File NewFile = new file (NewPath);

Filepart.writeto (NewFile);

Parammap.put ("Picname", NewFileName);
} else
return false; Description upload is not a picture
else ... {
Continue Description did not choose to upload pictures
}
else if (Part.isparam ()) ... {
Parampart Parampart = (parampart) part;

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.