欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中使用COS实现文件上传功能

程序员文章站 2024-02-22 09:07:58
cos是o'rrilly公司开发的一款用于http上传文件的opensource组件 需要cos.jar,下载地址: cos上传文件很简单,比fileupload还简单...

cos是o'rrilly公司开发的一款用于http上传文件的opensource组件

需要cos.jar,下载地址:

cos上传文件很简单,比fileupload还简单:但是上传最大我试了试,是800多兆,超过直接崩溃:

java.io.ioexception: posted content length of 1627105576 exceeds limit of 889192448 --->byte,800多m

Java中使用COS实现文件上传功能

只需一个servelt即可:

package com.lhy.upload;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.outputstream;
import java.io.printwriter;
import java.util.enumeration;
import java.util.uuid;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
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.filerenamepolicy;
/**
 * cosservlet 
 * 在cos中就一个类,
 * multipartrequest它是request的包装类。
 */
@webservlet(name="cosservlet",urlpatterns="/cosservlet")
public class cosservlet extends httpservlet{
 @override
 protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
  //第一步,声明文件的保存目录
  string path = getservletcontext().getrealpath("/up");
  //第二步:文件上传
  //声明文件重新命名策略,默认的不行不能重命名,自己实现filerenamepolicy接口
//  filerenamepolicy rename = new defaultfilerenamepolicy();
  multipartrequest multireq = new multipartrequest(req, path, 1024*1024*100, "utf-8",new myrename());
  //输出所上传的文件的信息 
  enumeration filenames = multireq.getfilenames(); 
  while(filenames.hasmoreelements()){ 
    string name = (string)filenames.nextelement(); 
    file file = multireq.getfile(name);//得到上传的文件
    if(null != file){ 
      string filename = multireq.getfilesystemname(name);  //取得文件名 
      string contenttype = multireq.getcontenttype(name);//类型
      system.out.println("上传的文件: " +filename+", 文件类型: "+contenttype); 
    } 
  } 
  //输出所提交的表单中其它文本输入域的值 
  enumeration formvalue = multireq.getparameternames(); 
  while(formvalue.hasmoreelements()){ 
    string param = (string)formvalue.nextelement(); 
    string value = multireq.getparameter(param); 
    system.out.println(value); 
  } 
  //第三步:如果知道input的name,还可以直接获取信息,
  /*resp.setcontenttype("text/html;charset=utf-8");
  printwriter out = resp.getwriter();
  out.print("文件名称1:"+multireq.getoriginalfilename("img1"));
  out.print("<br/>新名称:"+multireq.getfilesystemname("img1"));
  out.print("<br/>类型1:"+multireq.getcontenttype("img1"));
  out.print("<br/>大小1:"+multireq.getfile("img1").length());
  out.print("<br/>说明:"+multireq.getparameter("desc1"));
  if(multireq.getcontenttype("img1").contains("image/")){
   out.print("<img width='300px' height='200px' src='"+req.getcontextpath()+"/up/"+multireq.getfilesystemname("img1")+"'></img>");
  }
 out.print("<hr/>");
 out.print("文件名称2:"+multireq.getoriginalfilename("img2"));
 out.print("<br/>类型2:"+multireq.getcontenttype("img2"));
 out.print("<br/>大小2:"+multireq.getfile("img2").length());
 out.print("<br/>说明2:"+multireq.getparameter("desc2"));
// 
 out.print("<hr/>");
 out.print("文件名称3:"+multireq.getoriginalfilename("img3"));
 out.print("<br/>类型3:"+multireq.getcontenttype("img3"));
 out.print("<br/>大小3:"+multireq.getfile("img3").length());
 out.print("<br/>说明3:"+multireq.getparameter("desc3"));*/
 }
}
/**
 * 重命名策略,
 */
class myrename implements filerenamepolicy{
 @override
 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;
 }
}

表单:

<form action="<c:url value='/cosservlet'/>" method="post"
    enctype="multipart/form-data">
    file1:<input type="file" name="img1"><br /> 说明1:
    <input type="text" name="desc1"><br />
    file2:<input type="file" name="img2"><br/>
    说明2:<input type="text" name="desc2"><br/>
    file3:<input type="file" name="img3"><br/>
    说明3:<input type="text" name="desc3"><br/>
    <input type="submit" />
  </form>

启动上传:

Java中使用COS实现文件上传功能

服务端:

Java中使用COS实现文件上传功能

总结

以上所述是小编给大家介绍的java中使用cos实现文件上传功能,希望对大家有所帮助