Limit upload size, upload and download files with cos five (63)

Source: Internet
Author: User
Tags cos file copy

One limit upload size

1: Limit the total file size. If uploading 10 files, set the maximum total upload size to 100M.

void

setSizeMax(long sizeMax)
Sets the maximum allowed size of a complete request, as opposed to setFileSizeMax(long) .

2: Set the size of each file, if each file size is set to 10M.

 void

setFileSizeMax(long fileSizeMax)
Sets the maximum allowed size of a single uploaded file, as opposed to getSizeMax() .

Two use Cos to implement file upload

Package cn.hx;import java.io.file;import java.io.ioexception;import java.io.printwriter;import Java.util.UUID; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.oreilly.servlet.multipartrequest;import Com.oreilly.servlet.multipart.defaultfilerenamepolicy;import Com.oreilly.servlet.multipart.FileRenamePolicy;/** * in the Cos in a class, * multipartrequest It is the wrapper class of request. */ Public classCosservlet extends HttpServlet { Public voidDoPost (httpservletrequest request, HttpServletResponse resp) throws Servletexception, IOException {//first step: Declaring the file's save directoryString Path= Getservletcontext (). Getrealpath ("/up"); //Step Two: File transfer//policy for declaring a file to be re-namedfilerenamepolicy Rename=NewDefaultfilerenamepolicy (); Multipartrequest req=NewMultipartrequest (Request,path,1024x768*1024x768* -,"UTF-8",Newmyrename ());//     //Step Three: Display the information,Resp.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Resp.getwriter ();  out. Print ("file name 1:"+req.getoriginalfilename ("IMG1"));  out. Print ("<br/> new name:"+req.getfilesystemname ("IMG1"));  out. Print ("<br/> Type 1:"+req.getcontenttype ("IMG1"));  out. Print ("<br/> size 1:"+req.getfile ("IMG1"). Length ());  out. Print ("<br/> Description:"+req.getparameter ("DESC1")); if(Req.getcontenttype ("IMG1"). Contains ("image/")){            out. Print (""+request.getcontextpath () +"/up/"+req.getfilesystemname ("IMG1")+"' ></img>"); }    }}classMyrename implements filerenamepolicy{ Publicfile Rename (file file) {String fileName=File.getname (); String Extname= Filename.substring (Filename.lastindexof (".")); String UUID= Uuid.randomuuid (). toString (). Replace ("-",""); String NewName= Uuid+extname;//abc.jpgfile=NewFile (File.getparent (), newName); returnfile; }}

Three Downloads

Can be either a get or post.

Public void DoPost (httpservletrequest req, HttpServletResponse resp)

throws Servletexception, IOException {

Req.setcharacterencoding ("UTF-8");

String name = Req.getparameter ("name");

First step: Set the type of response

Resp.setcontenttype ("Application/force-download");

Second Read file

String path = Getservletcontext (). Getrealpath ("/up/" +name);

InputStream in = new fileinputstream (path);

Setting the response header

URL encoding a file name

Name = Urlencoder. encode (Name, "UTF-8");

Resp.setheader ("Content-disposition", "attachment;filename=" +name);

Resp.setcontentlength (In.available ());

Step three: Start file copy

OutputStream out = Resp.getoutputstream ();

byte [] B = new byte[1024];

int len = 0;

while ((Len=in.read (b))!=-1) {

Out.write (B,0,len);

}

Out.close ();

In.close ();

}

Limit upload size, upload and download files with cos five (63)

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.