你的位置:首页 > Java教程

[Java教程]限制上传大小、用COS实现文件上传与下载 五(63)

一 限制上传大小

1:限制总文件的大小 。    如 上传10文件,设置最多总上传大小为100M。

void

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

2:设置第每一个文件的大小 ,如果设置每 一个文件大小10M。

 

 void

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

 二 用COS实现文件上传

 

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;/** * 在Cos中就一个类, * MultipartRequest它是request的包装类。 */public class CosServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse resp)throws ServletException, IOException {    //第一步:声明文件的保存目录    String path = getServletContext().getRealPath("/up");    //第二步:文件传    //声明文件重新取名的策略    FileRenamePolicy rename = new DefaultFileRenamePolicy();    MultipartRequest req =       new MultipartRequest(request,path,1024*1024*100,"UTF-8",new MyRename());//   //第三步:显示信息,    resp.setContentType("text/html;charset=UTf-8");    PrintWriter out = resp.getWriter();    out.print("文件名称1:"+req.getOriginalFileName("img1"));    out.print("<br/>新名称:"+req.getFilesystemName("img1"));    out.print("<br/>类型1:"+req.getContentType("img1"));    out.print("<br/>大小1:"+req.getFile("img1").length());    out.print("<br/>说明:"+req.getParameter("desc1"));    if(req.getContentType("img1").contains("image/")){      out.print("<img src="/images/loading4.gif" data-original='"+request.getContextPath()+"/up/"+req.getFilesystemName("img1")+"'></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;  }}

三 下载

即可是get也可以是post。

    public void doPost(HttpServletRequest req, HttpServletResponse resp)

           throws ServletException, IOException {

       req.setCharacterEncoding("UTF-8");

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

       //第一步:设置响应的类型

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

       //第二读取文件

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

       InputStream in = new FileInputStream(path);

       //设置响应头

       //对文件名进行url编码

       name = URLEncoder.encode(name, "UTF-8");

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

       resp.setContentLength(in.available());

       //第三步:开始文件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();

    }