STRUTS2 Integrated COS

Source: Internet
Author: User

STRUTS2 because license reason long ago no longer support COS upload, specifically see http://struts.apache.org/2.1.6/docs/file-upload.html official note:
there is a third alternative, COS, but it is removed due to licensing incompatibilities.

Currently, Commons-fileupload is embedded and supported by plugin mode Pell. But Struts.properties is still not updated, and the articles on the web also remain by modifying the struts.multipart.parser corresponding values for the Cos to come from the dynamic support Cos.

Let's not consider the license factor and see how STRUTS2 can support cos:
1) Create a class that implements Org.apache.struts2.dispatcher.multipart.MultiPartRequest excuses

Java code
  1. /**
  2. * @author <a href= "http://intolong.spaces.live.com/" >Jalen</a>
  3. */
  4. Public class Cosmultipartrequest implements Multipartrequest {
  5. private Multipartrequest Multi;
  6. private String defaultencoding;
  7. private Boolean maxsizeprovided;
  8. private int maxSize;
  9. @Inject (strutsconstants.struts_i18n_encoding)
  10. public void setdefaultencoding (String defaultencoding) {
  11. this.defaultencoding = defaultencoding;
  12. }
  13. @Inject (strutsconstants.struts_multipart_maxsize)
  14. public void Setmaxsize (String maxSize) {
  15. this.maxsizeprovided = true;
  16. this.maxsize = Integer.parseint (maxSize);
  17. }
  18. @Override
  19. Public string[] getContentType (String fieldName) {
  20. return new string[] {Multi.getcontenttype (fieldName)};
  21. }
  22. @SuppressWarnings ("unchecked")
  23. @Override
  24. Public List geterrors () {
  25. return collections.empty_list;
  26. }
  27. @Override
  28. Public file[] GetFile (String fieldName) {
  29. return new file[] {multi.getfile (fieldName)};
  30. }
  31. @Override
  32. Public string[] GetFileNames (String fieldName) {
  33. return new string[] {multi.getfile (fieldName). GetName ()};
  34. }
  35. @SuppressWarnings ("unchecked")
  36. @Override
  37. Public enumeration<string> Getfileparameternames () {
  38. return Multi.getfilenames ();
  39. }
  40. @Override
  41. Public string[] Getfilesystemname (String name) {
  42. return new string[] {multi.getfilesystemname (name)};
  43. }
  44. @Override
  45. Public string GetParameter (string name) {
  46. return Multi.getparameter (name);
  47. }
  48. @SuppressWarnings ("unchecked")
  49. @Override
  50. Public enumeration<string> Getparameternames () {
  51. return Multi.getparameternames ();
  52. }
  53. @Override
  54. Public string[] Getparametervalues (String name) {
  55. return multi.getparametervalues (name);
  56. }
  57. @Override
  58. public Void Parse (httpservletrequest request, String Savedir) throws IOException {
  59. if (maxsizeprovided) {
  60. Multi = new Multipartrequest (Request, Savedir, maxSize, defaultencoding);
  61. } Else {
  62. Multi = new Multipartrequest (Request, Savedir, defaultencoding);
  63. }
  64. }
  65. }



2) Modify the Struts.xml to specify your parser

Java code
    1. <constant name="Struts.multipart.parser" value="cos"/>


This can also be done by modifying the struts.properties to the same effect.


3) Modify Struts.xml, register your parser

Java code
    1. <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="cos" class=" Jalen.core.struts2.CosMultiPartRequest "/>



Done.


Following the performance comparison of a Cos and other upload lib, I did not test myself:

Processing HTTP-based file uploads, there are so few opensource Lib,o ' Reilly Cos,apache FileUpload, and Jspsmart's smartupload.

On-line check their performance comparison, excerpt as follows:

Using 2M, 20M, 45M, 200M upload size to test the time spent in three components (in MS) results are:

2M

536

 

1th time

2nd time

3rd time

4th time

Average

Cos

297

203

234

245

245

FileUpload

281

312

281

31 2

297

smartupload

< p>531

594

485

532

20M

 

1th time

2nd time

3rd time

4th time

Average

Cos

2562

2109

2719

2172

2391

FileUpload

4062

4140

536 0

3922

4371

Smar Tupload

3453

3094

3078

3547

3293

45M

 

1th time

2nd time

3rd time

4th time

Average

Cos

4860

4844

5125

5171

9

FileUpload

9000

8391

10375

10078

9461

Smart Upload

8265

9187

8672

8856

8745

200M

1th time

2nd time

3rd time

4th time

Average

Cos

55813

52282

54796

51187

53520

FileUpload

76343

68531

80954

79031

76215

Smartupload

Memory Stack Overflow

From the above table comparison, we can see that Cos always maintains good performance. When the upload volume is small (capacity <2m, which is the most common case), COS is not much better than fileupload performance, but Smartupload has begun to show weakness. As the capacity increases, fileupload and smartupload performance drops very fast, until the 200M capacity, Smartupload has been overwhelmed by the crash, and Cos at this time spent less than fileupload more than 20 seconds, cannot but say in this evaluation , Cos has the highest performance.

Struts2 integrated cos (RPM)

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.