Restrict the upload size, use COS to upload and download files 5 (63), and use cos to upload files

Source: Internet
Author: User

Restrict the upload size, use COS to upload and download files 5 (63), and use cos to upload files

I. Restrict upload size

1: Limit the total file size. For example, to upload 10 files, set the total upload size to 100 MB.

void

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

2: set the size of each file. If the size of each file is set to 10 MB.

 

 void

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

2. Use COS to upload files

 

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;/*** is a class in Cos. * MultipartRequest is the request packaging class. */Public class CosServlet extends HttpServlet {public void doPost (HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {// Step 1: declare the file storage directory String path = getServletContext (). getRealPath ("/up"); // Step 2: File Transfer // declare FileRenamePolicy rename = new DefaultFileRenamePolicy (); MultipartRequest req = new MultipartRequest (request, path, 1024*1024*100, "UTF-8", new MyRename (); // Step 3: Display 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/> note:" + req. getParameter ("desc1"); if (req. getContentType ("img1 "). contains ("image/") {out. print (" </img>") ;}} class MyRename implements FileRenamePolicy {public File rename (File file) {String fileName = file. getName (); String extName = fileName. substring (fileName. lastIndexOf (". "); String uuid = UUID. randomUUID (). toString (). replace ("-", ""); String newName = uuid + extName; // abc.jpg file = new File (file. getParent (), newName); return file ;}}

Download 3

That is, get can also be post.

Public VoidDoPost (HttpServletRequest req, HttpServletResponse resp)

ThrowsServletException, IOException {

Req. setCharacterEncoding ("UTF-8 ");

String name = req. getParameter ("name ");

// Step 1: Set the response type

Resp. setContentType ("application/force-download ");

// Second read the file

String path = getServletContext (). getRealPath ("/up/" + name );

InputStream in =NewFileInputStream (path );

// Set the Response Header

// Perform url encoding on the file name

Name = URLEncoder.Encode(Name, "UTF-8 ");

Resp. setHeader ("Content-Disposition", "attachment; filename =" + name );

Resp. setContentLength (in. available ());

// Step 3: Start file copy

OutputStream out = resp. getOutputStream ();

Byte[] B =New Byte[1024];

IntLen = 0;

While(Len = in. read (B ))! =-1 ){

Out. write (B, 0, len );

}

Out. close ();

In. close ();

}

 

 

 

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.