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

时间:2022-09-23 10:57:37

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

需要cos.jar,下载地址:http://www.servlets.com/cos/

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

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

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

只需一个servelt即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
 }
}

表单:

?
1
2
3
4
5
6
7
8
9
10
<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实现文件上传功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/lihaoyang/archive/2017/08/11/7346495.html