1 jspsmart之upload组件源码主要包括了5个java文件
2
3 file.java
4
5 import java.io.bytearrayinputstream;
6 import java.io.fileoutputstream;
7 import java.io.ioexception;
8 import java.math.biginteger;
9 import java.sql.resultset;
10 import java.sql.sqlexception;
11 import javax.servlet.servletexception;
12
13 // referenced classes of package com.jspsmart.upload:
14 // smartuploadexception, smartupload
15
16 public class file
17 {
18
19 private smartupload m_parent;
20 private int m_startdata;
21 private int m_enddata;
22 private int m_size;
23 private string m_fieldname;
24 private string m_filename;
25 private string m_fileext;
26 private string m_filepathname;
27 private string m_contenttype;
28 private string m_contentdisp;
29 private string m_typemime;
30 private string m_subtypemime;
31 private string m_contentstring;
32 private boolean m_ismissing;
33 public static final int saveas_auto = 0;
34 public static final int saveas_virtual = 1;
35 public static final int saveas_physical = 2;
36
37 file()
38 {
39 m_startdata = 0;
40 m_enddata = 0;
41 m_size = 0;
42 m_fieldname = new string();
43 m_filename = new string();
44 m_fileext = new string();
45 m_filepathname = new string();
46 m_contenttype = new string();
47 m_contentdisp = new string();
48 m_typemime = new string();
49 m_subtypemime = new string();
50 m_contentstring = new string();
51 m_ismissing = true;
52 }
53
54 public void saveas(string s)
55 throws ioexception, smartuploadexception
56 {
57 saveas(s, 0);
58 }
59
60 public void saveas(string s, int i)
61 throws ioexception, smartuploadexception
62 {
63 string s1 = new string();
64 s1 = m_parent.getphysicalpath(s, i);
65 if(s1 == null)
66 throw new illegalargumentexception("there is no specified destination file (1140).");
67 try
68 {
69 java.io.file file = new java.io.file(s1);
70 fileoutputstream fileoutputstream = new fileoutputstream(file);
71 fileoutputstream.write(m_parent.m_binarray, m_startdata, m_size);
72 fileoutputstream.close();
73 }
74 catch(ioexception ioexception)
75 {
76 throw new smartuploadexception("file cant be saved (1120).");
77 }
78 }
79
80 public void filetofield(resultset resultset, string s)
81 throws servletexception, ioexception, smartuploadexception, sqlexception
82 {
83 long l = 0l;
84 int i = 0x10000;
85 int j = 0;
86 int k = m_startdata;
87 if(resultset == null)
88 throw new illegalargumentexception("the recordset cannot be null (1145).");
89 if(s == null)
90 throw new illegalargumentexception("the columnname cannot be null (1150).");
91 if(s.length() == 0)
92 throw new illegalargumentexception("the columnname cannot be empty (1155).");
93 l = biginteger.valueof(m_size).divide(biginteger.valueof(i)).longvalue();
94 j = biginteger.valueof(m_size).mod(biginteger.valueof(i)).intvalue();
95 try
96 {
97 for(int i1 = 1; (long)i1 < l; i1++)
98 {
99 resultset.updatebinarystream(s, new bytearrayinputstream(m_parent.m_binarray, k, i), i);
100 k = k != 0 ? k : 1;
101 k = i1 * i + m_startdata;
102 }
103
104 if(j > 0)
105 resultset.updatebinarystream(s, new bytearrayinputstream(m_parent.m_binarray, k, j), j);
106 }
107 catch(sqlexception sqlexception)
108 {
109 byte abyte0[] = new byte[m_size];
110 system.arraycopy(m_parent.m_binarray, m_startdata, abyte0, 0, m_size);
111 resultset.updatebytes(s, abyte0);
112 }
113 catch(exception exception)
114 {
115 throw new smartuploadexception("unable to save file in the database (1130).");
116 }
117 }
118
119 public boolean ismissing()
120 {
121 return m_ismissing;
122 }
123
124 public string getfieldname()
125 {
126 return m_fieldname;
127 }
128
129 public string getfilename()
130 {
131 return m_filename;
132 }
133
134 public string getfilepathname()
135 {
136 return m_filepathname;
137 }
138
139 public string getfileext()
140 {
141 return m_fileext;
142 }
143
144 public string getcontenttype()
145 {
146 return m_contenttype;
147 }
148
149 public string getcontentdisp()
150 {
151 return m_contentdisp;
152 }
153
154 public string getcontentstring()
155 {
156 string s = new string(m_parent.m_binarray, m_startdata, m_size);
157 return s;
158 }
159
160 public string gettypemime()
161 throws ioexception
162 {
163 return m_typemime;
164 }
165
166 public string getsubtypemime()
167 {
168 return m_subtypemime;
169 }
170
171 public int getsize()
172 {
173 return m_size;
174 }
175
176 protected int getstartdata()
177 {
178 return m_startdata;
179 }
180
181 protected int getenddata()
182 {
183 return m_enddata;
184 }
185
186 protected void setparent(smartupload smartupload)
187 {
188 m_parent = smartupload;
189 }
190
191 protected void setstartdata(int i)
192 {
193 m_startdata = i;
194 }
195
196 protected void setenddata(int i)
197 {
198 m_enddata = i;
199 }
200
201 protected void setsize(int i)
202 {
203 m_size = i;
204 }
205
206 protected void setismissing(boolean flag)
207 {
208 m_ismissing = flag;
209 }
210
211 protected void setfieldname(string s)
212 {
213 m_fieldname = s;
214 }
215
216 protected void setfilename(string s)
217 {
218 m_filename = s;
219 }
220
221 protected void setfilepathname(string s)
222 {
223 m_filepathname = s;
224 }
225
226 protected void setfileext(string s)
227 {
228 m_fileext = s;
229 }
230
231 protected void setcontenttype(string s)
232 {
233 m_contenttype = s;
234 }
235
236 protected void setcontentdisp(string s)
237 {
238 m_contentdisp = s;
239 }
240
241 protected void settypemime(string s)
242 {
243 m_typemime = s;
244 }
245
246 protected void setsubtypemime(string s)
247 {
248 m_subtypemime = s;
249 }
250
251 public byte getbinarydata(int i)
252 {
253 if(m_startdata + i > m_enddata)
254 throw new arrayindexoutofboundsexception("index out of range (1115).");
255 if(m_startdata + i <= m_enddata)
256 return m_parent.m_binarray[m_startdata + i];
257 else
258 return 0;
259 }
260 }
261
262
263
264
265 files.java
266
267 import java.io.ioexception;
268 import java.util.*;
269
270 // referenced classes of package com.jspsmart.upload:
271 // file, smartupload
272
273 public class files
274 {
275
276 private smartupload m_parent;
277 private hashtable m_files;
278 private int m_counter;
279
280 files()
281 {
282 m_files = new hashtable();
283 m_counter = 0;
284 }
285
286 protected void addfile(file file)
287 {
288 if(file == null)
289 {
290 throw new illegalargumentexception("newfile cannot be null.");
291 } else
292 {
293 m_files.put(new integer(m_counter), file);
294 m_counter++;
295 return;
296 }
297 }
298
299 public file getfile(int i)
300 {
301 if(i < 0)
302 throw new illegalargumentexception("files index cannot be a negative value (1210).");
303 file file = (file)m_files.get(new integer(i));
304 if(file == null)
305 throw new illegalargumentexception("files name is invalid or does not exist (1205).");
306 else
307 return file;
308 }
309
310 public int getcount()
311 {
312 return m_counter;
313 }
314
315 public long getsize()
316 throws ioexception
317 {
318 long l = 0l;
319 for(int i = 0; i < m_counter; i++)
320 l += getfile(i).getsize();
321
322 return l;
323 }
324
325 public collection getcollection()
326 {
327 return m_files.values();
328 }
329
330 public enumeration getenumeration()
331 {
332 return m_files.elements();
333 }
334 }
335
336
337
338
339 request.java
340
341 import java.util.enumeration;
342 import java.util.hashtable;
343
344 public class request
345 {
346
347 private hashtable m_parameters;
348 private int m_counter;
349
350 request()
351 {
352 m_parameters = new hashtable();
353 m_counter = 0;
354 }
355
356 protected void putparameter(string s, string s1)
357 {
358 if(s == null)
359 throw new illegalargumentexception("the name of an element cannot be null.");
360 if(m_parameters.containskey(s))
361 {
362 hashtable hashtable = (hashtable)m_parameters.get(s);
363 hashtable.put(new integer(hashtable.size()), s1);
364 } else
365 {
366 hashtable hashtable1 = new hashtable();
367 hashtable1.put(new integer(0), s1);
368 m_parameters.put(s, hashtable1);
369 m_counter++;
370 }
371 }
372
373 public string getparameter(string s)
374 {
375 if(s == null)
376 throw new illegalargumentexception("forms name is invalid or does not exist (1305).");
377 hashtable hashtable = (hashtable)m_parameters.get(s);
378 if(hashtable == null)
379 return null;
380 else
381 return (string)hashtable.get(new integer(0));
382 }
383
384 public enumeration getparameternames()
385 {
386 return m_parameters.keys();
387 }
388
389 public string[] getparametervalues(string s)
390 {
391 if(s == null)
392 throw new illegalargumentexception("forms name is invalid or does not exist (1305).");
393 hashtable hashtable = (hashtable)m_parameters.get(s);
394 if(hashtable == null)
395 return null;
396 string as[] = new string[hashtable.size()];
397 for(int i = 0; i < hashtable.size(); i++)
398 as[i] = (string)hashtable.get(new integer(i));
399
400 return as;
401 }
402 }
403
404
405
406
407 smartuploadexception .java
408
409 public class smartuploadexception extends exception
410 {
411
412 smartuploadexception(string s)
413 {
414 super(s);
415 }
416 }
417
418
419
420
421 smartupload.java
422
423 import java.io.*;
424 import java.sql.resultset;
425 import java.sql.sqlexception;
426 import java.util.vector;
427 import javax.servlet.*;
428 import javax.servlet.http.*;
429 import javax.servlet.jsp.jspwriter;
430 import javax.servlet.jsp.pagecontext;
431
432 // referenced classes of package com.jspsmart.upload:
433 // files, request, smartuploadexception, file
434
435 public class smartupload
436 {
437
438 protected byte m_binarray[];
439 protected httpservletrequest m_request;
440 protected httpservletresponse m_response;
441 protected servletcontext m_application;
442 private int m_totalbytes;
443 private int m_currentindex;
444 private int m_startdata;
445 private int m_enddata;
446 private string m_boundary;
447 private long m_totalmaxfilesize;
448 private long m_maxfilesize;
449 private vector m_deniedfileslist;
450 private vector m_allowedfileslist;
451 private boolean m_denyphysicalpath;
452 private boolean m_forcephysicalpath;
453 private string m_contentdisposition;
454 public static final int save_auto = 0;
455 public static final int save_virtual = 1;
456 public static final int save_physical = 2;
457 private files m_files;
458 private request m_formrequest;
459
460 public smartupload()
461 {
462 m_totalbytes = 0;
463 m_currentindex = 0;
464 m_startdata = 0;
465 m_enddata = 0;
466 m_boundary = new string();
467 m_totalmaxfilesize = 0l;
468 m_maxfilesize = 0l;
469 m_deniedfileslist = new vector();
470 m_allowedfileslist = new vector();
471 m_denyphysicalpath = false;
472 m_forcephysicalpath = false;
473 m_contentdisposition = new string();
474 m_files = new files();
475 m_formrequest = new request();
476 }
477
478 public final void init(servletconfig servletconfig)
479 throws servletexception
480 {
481 m_application = servletconfig.getservletcontext();
482 }
483
484 public void service(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse)
485 throws servletexception, ioexception
486 {
487 m_request = httpservletrequest;
488 m_response = httpservletresponse;
489 }
490
491 public final void initialize(servletconfig servletconfig, httpservletrequest httpservletrequest, httpservletresponse httpservletresponse)
492 throws servletexception
493 {
494 m_application = servletconfig.getservletcontext();
495 m_request = httpservletrequest;
496 m_response = httpservletresponse;
497 }
498
499 public final void initialize(pagecontext pagecontext)
500 throws servletexception
501 {
502 m_application = pagecontext.getservletcontext();
503 m_request = (httpservletrequest)pagecontext.getrequest();
504 m_response = (httpservletresponse)pagecontext.getresponse();
505 }
506
507 public final void initialize(servletcontext servletcontext, httpsession httpsession, httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, jspwriter jspwriter)
508 throws servletexception
509 {
510 m_application = servletcontext;
511 m_request = httpservletrequest;
512 m_response = httpservletresponse;
513 }
514
515 public void upload()
516 throws servletexception, ioexception, smartuploadexception
517 {
518 int i = 0;
519 boolean flag = false;
520 long l = 0l;
521 boolean flag1 = false;
522 string s = new string();
523 string s2 = new string();
524 string s4 = new string();
525 string s5 = new string();
526 string s6 = new string();
527 string s7 = new string();
528 string s8 = new string();
529 string s9 = new string();
530 string s10 = new string();
531 boolean flag2 = false;
532 m_totalbytes = m_request.getcontentlength();
533 m_binarray = new byte[m_totalbytes];
534 int j;
535 for(; i < m_totalbytes; i += j)
536 try
537 {
538 m_request.getinputstream();
539 j = m_request.getinputstream().read(m_binarray, i, m_totalbytes - i);
540 }
541 catch(exception exception)
542 {
543 throw new smartuploadexception("unable to upload.");
544 }
545
546 for(; !flag1 && m_currentindex < m_totalbytes; m_currentindex++)
547 if(m_binarray[m_currentindex] == 13)
548 flag1 = true;
549 else
550 m_boundary = m_boundary + (char)m_binarray[m_currentindex];
551
552 if(m_currentindex == 1)
553 return;
554 for(m_currentindex++; m_currentindex < m_totalbytes; m_currentindex = m_currentindex + 2)
555 {
556 string s1 = getdataheader();
557 m_currentindex = m_currentindex + 2;
558 boolean flag3 = s1.indexof("filename") > 0;
559 string s3 = getdatafieldvalue(s1, "name");
560 if(flag3)
561 {
562 s6 = getdatafieldvalue(s1, "filename");
563 s4 = getfilename(s6);
564 s5 = getfileext(s4);
565 s7 = getcontenttype(s1);
566 s8 = getcontentdisp(s1);
567 s9 = gettypemime(s7);
568 s10 = getsubtypemime(s7);
569 }
570 getdatasection();
571 if(flag3 && s4.length() > 0)
572 {
573 if(m_deniedfileslist.contains(s5))
574 throw new securityexception("the extension of the file is denied to be uploaded (1015).");
575 if(!m_allowedfileslist.isempty() && !m_allowedfileslist.contains(s5))
576 throw new securityexception("the extension of the file is not allowed to be uploaded (1010).");
577 if(m_maxfilesize > 0l && (long)((m_enddata - m_startdata) + 1) > m_maxfilesize)
578 throw new securityexception("size exceeded for this file : " + s4 + " (1105).");
579 l += (m_enddata - m_startdata) + 1;
580 if(m_totalmaxfilesize > 0l && l > m_totalmaxfilesize)
581 throw new securityexception("total file size exceeded (1110).");
582 }
583 if(flag3)
584 {
585 com.facet.jspsmart.upload.file file = new com.facet.jspsmart.upload.file();
586 file.setparent(this);
587 file.setfieldname(s3);
588 file.setfilename(s4);
589 file.setfileext(s5);
590 file.setfilepathname(s6);
591 file.setismissing(s6.length() == 0);
592 file.setcontenttype(s7);
593 file.setcontentdisp(s8);
594 file.settypemime(s9);
595 file.setsubtypemime(s10);
596 if(s7.indexof("application/x-macbinary") > 0)
597 m_startdata = m_startdata + 128;
598 file.setsize((m_enddata - m_startdata) + 1);
599 file.setstartdata(m_startdata);
600 file.setenddata(m_enddata);
601 m_files.addfile(file);
602 } else
603 {
604 string s11 = new string(m_binarray, m_startdata, (m_enddata - m_startdata) + 1);
605 m_formrequest.putparameter(s3, s11);
606 }
607 if((char)m_binarray[m_currentindex + 1] == -)
608 break;
609 }
610
611 }
612
613 public int save(string s)
614 throws servletexception, ioexception, smartuploadexception
615 {
616 return save(s, 0);
617 }
618
619 public int save(string s, int i)
620 throws servletexception, ioexception, smartuploadexception
621 {
622 int j = 0;
623 if(s == null)
624 s = m_application.getrealpath("/");
625 if(s.indexof("/") != -1)
626 {
627 if(s.charat(s.length() - 1) != /)
628 s = s + "/";
629 } else
630 if(s.charat(s.length() - 1) != \\)
631 s = s + "\\";
632 for(int k = 0; k < m_files.getcount(); k++)
633 if(!m_files.getfile(k).ismissing())
634 {
635 m_files.getfile(k).saveas(s + m_files.getfile(k).getfilename(), i);
636 j++;
637 }
638
639 return j;
640 }
641
642 public int getsize()
643 {
644 return m_totalbytes;
645 }
646
647 public byte getbinarydata(int i)
648 {
649 byte byte0;
650 try
651 {
652 byte0 = m_binarray[i];
653 }
654 catch(exception exception)
655 {
656 throw new arrayindexoutofboundsexception("index out of range (1005).");
657 }
658 return byte0;
659 }
660
661 public files getfiles()
662 {
663 return m_files;
664 }
665
666 public request getrequest()
667 {
668 return m_formrequest;
669 }
670
671 public void downloadfile(string s)
672 throws servletexception, ioexception, smartuploadexception
673 {
674 downloadfile(s, null, null);
675 }
676
677 public void downloadfile(string s, string s1)
678 throws servletexception, ioexception, smartuploadexception, smartuploadexception
679 {
680 downloadfile(s, s1, null);
681 }
682
683 public void downloadfile(string s, string s1, string s2)
684 throws servletexception, ioexception, smartuploadexception
685 {
686 downloadfile(s, s1, s2, 65000);
687 }
688
689 public void downloadfile(string s, string s1, string s2, int i)
690 throws servletexception, ioexception, smartuploadexception
691 {
692 if(s == null)
693 throw new illegalargumentexception("file " + s + " not found (1040).");
694 if(s.equals(""))
695 throw new illegalargumentexception("file " + s + " not found (1040).");
696 if(!isvirtual(s) && m_denyphysicalpath)
697 throw new securityexception("physical path is denied (1035).");
698 if(isvirtual(s))
699 s = m_application.getrealpath(s);
700 java.io.file file = new java.io.file(s);
701 fileinputstream fileinputstream = new fileinputstream(file);
702 long l = file.length();
703 boolean flag = false;
704 int k = 0;
705 byte abyte0[] = new byte[i];
706 if(s1 == null)
707 m_response.setcontenttype("application/x-msdownload");
708 else
709 if(s1.length() == 0)
710 m_response.setcontenttype("application/x-msdownload");
711 else
712 m_response.setcontenttype(s1);
713 m_response.setcontentlength((int)l);
714 m_contentdisposition = m_contentdisposition != null ? m_contentdisposition : "attachment;";
715 if(s2 == null)
716 m_response.setheader("content-disposition", m_contentdisposition + " filename=" + getfilename(s));
717 else
718 if(s2.length() == 0)
719 m_response.setheader("content-disposition", m_contentdisposition);
720 else
721 m_response.setheader("content-disposition", m_contentdisposition + " filename=" + s2);
722 while((long)k < l)
723 {
724 int j = fileinputstream.read(abyte0, 0, i);
725 k += j;
726 m_response.getoutputstream().write(abyte0, 0, j);
727 }
728 fileinputstream.close();
729 }
730
731 public void downloadfield(resultset resultset, string s, string s1, string s2)
732 throws servletexception, ioexception, sqlexception
733 {
734 if(resultset == null)
735 throw new illegalargumentexception("the recordset cannot be null (1045).");
736 if(s == null)
737 throw new illegalargumentexception("the columnname cannot be null (1050).");
738 if(s.length() == 0)
739 throw new illegalargumentexception("the columnname cannot be empty (1055).");
740 byte abyte0[] = resultset.getbytes(s);
741 if(s1 == null)
742 m_response.setcontenttype("application/x-msdownload");
743 else
744 if(s1.length() == 0)
745 m_response.setcontenttype("application/x-msdownload");
746 else
747 m_response.setcontenttype(s1);
748 m_response.setcontentlength(abyte0.length);
749 if(s2 == null)
750 m_response.setheader("content-disposition", "attachment;");
751 else
752 if(s2.length() == 0)
753 m_response.setheader("content-disposition", "attachment;");
754 else
755 m_response.setheader("content-disposition", "attachment; filename=" + s2);
756 m_response.getoutputstream().write(abyte0, 0, abyte0.length);
757 }
758
759 public void fieldtofile(resultset resultset, string s, string s1)
760 throws servletexception, ioexception, smartuploadexception, sqlexception
761 {
762 try
763 {
764 if(m_application.getrealpath(s1) != null)
765 s1 = m_application.getrealpath(s1);
766 inputstream inputstream = resultset.getbinarystream(s);
767 fileoutputstream fileoutputstream = new fileoutputstream(s1);
768 int i;
769 while((i = inputstream.read()) != -1)
770 fileoutputstream.write(i);
771 fileoutputstream.close();
772 }
773 catch(exception exception)
774 {
775 throw new smartuploadexception("unable to save file from the database (1020).");
776 }
777 }
778
779 private string getdatafieldvalue(string s, string s1)
780 {
781 string s2 = new string();
782 string s3 = new string();
783 int i = 0;
784 boolean flag = false;
785 boolean flag1 = false;
786 boolean flag2 = false;
787 s2 = s1 + "=" + ";
788 i = s.indexof(s2);
789 if(i > 0)
790 {
791 int j = i + s2.length();
792 int k = j;
793 s2 = "\"";
794 int l = s.indexof(s2, j);
795 if(k > 0 && l > 0)
796 s3 = s.substring(k, l);
797 }
798 return s3;
799 }
800
801 private string getfileext(string s)
802 {
803 string s1 = new string();
804 int i = 0;
805 int j = 0;
806 if(s == null)
807 return null;
808 i = s.lastindexof(46) + 1;
809 j = s.length();
810 s1 = s.substring(i, j);
811 if(s.lastindexof(46) > 0)
812 return s1;
813 else
814 return "";
815 }
816
817 private string getcontenttype(string s)
818 {
819 string s1 = new string();
820 string s2 = new string();
821 int i = 0;
822 boolean flag = false;
823 s1 = "content-type:";
824 i = s.indexof(s1) + s1.length();
825 if(i != -1)
826 {
827 int j = s.length();
828 s2 = s.substring(i, j);
829 }
830 return s2;
831 }
832
833 private string gettypemime(string s)
834 {
835 string s1 = new string();
836 int i = 0;
837 i = s.indexof("/");
838 if(i != -1)
839 return s.substring(1, i);
840 else
841 return s;
842 }
843
844 private string getsubtypemime(string s)
845 {
846 string s1 = new string();
847 int i = 0;
848 boolean flag = false;
849 i = s.indexof("/") + 1;
850 if(i != -1)
851 {
852 int j = s.length();
853 return s.substring(i, j);
854 } else
855 {
856 return s;
857 }
858 }
859
860 private string getcontentdisp(string s)
861 {
862 string s1 = new string();
863 int i = 0;
864 int j = 0;
865 i = s.indexof(":") + 1;
866 j = s.indexof(";");
867 s1 = s.substring(i, j);
868 return s1;
869 }
870
871 private void getdatasection()
872 {
873 boolean flag = false;
874 string s = new string();
875 int i = m_currentindex;
876 int j = 0;
877 int k = m_boundary.length();
878 m_startdata = m_currentindex;
879 m_enddata = 0;
880 while(i < m_totalbytes)
881 if(m_binarray[i] == (byte)m_boundary.charat(j))
882 {
883 if(j == k - 1)
884 {
885 m_enddata = ((i - k) + 1) - 3;
886 break;
887 }
888 i++;
889 j++;
890 } else
891 {
892 i++;
893 j = 0;
894 }
895 m_currentindex = m_enddata + k + 3;
896 }
897
898 private string getdataheader()
899 {
900 int i = m_currentindex;
901 int j = 0;
902 boolean flag = false;
903 for(boolean flag1 = false; !flag1;)
904 if(m_binarray[m_currentindex] == 13 && m_binarray[m_currentindex + 2] == 13)
905 {
906 flag1 = true;
907 j = m_currentindex - 1;
908 m_currentindex = m_currentindex + 2;
909 } else
910 {
911 m_currentindex++;
912 }
913
914 string s = new string(m_binarray, i, (j - i) + 1);
915 return s;
916 }
917
918 private string getfilename(string s)
919 {
920 string s1 = new string();
921 string s2 = new string();
922 int i = 0;
923 boolean flag = false;
924 boolean flag1 = false;
925 boolean flag2 = false;
926 i = s.lastindexof(47);
927 if(i != -1)
928 return s.substring(i + 1, s.length());
929 i = s.lastindexof(92);
930 if(i != -1)
931 return s.substring(i + 1, s.length());
932 else
933 return s;
934 }
935
936 public void setdeniedfileslist(string s)
937 throws servletexception, ioexception, sqlexception
938 {
939 string s1 = "";
940 if(s != null)
941 {
942 string s2 = "";
943 for(int i = 0; i < s.length(); i++)
944 if(s.charat(i) == ,)
945 {
946 if(!m_deniedfileslist.contains(s2))
947 m_deniedfileslist.addelement(s2);
948 s2 = "";
949 } else
950 {
951 s2 = s2 + s.charat(i);
952 }
953
954 if(s2 != "")
955 m_deniedfileslist.addelement(s2);
956 } else
957 {
958 m_deniedfileslist = null;
959 }
960 }
961
962 public void setallowedfileslist(string s)
963 {
964 string s1 = "";
965 if(s != null)
966 {
967 string s2 = "";
968 for(int i = 0; i < s.length(); i++)
969 if(s.charat(i) == ,)
970 {
971 if(!m_allowedfileslist.contains(s2))
972 m_allowedfileslist.addelement(s2);
973 s2 = "";
974 } else
975 {
976 s2 = s2 + s.charat(i);
977 }
978
979 if(s2 != "")
980 m_allowedfileslist.addelement(s2);
981 } else
982 {
983 m_allowedfileslist = null;
984 }
985 }
986
987 public void setdenyphysicalpath(boolean flag)
988 {
989 m_denyphysicalpath = flag;
990 }
991
992 public void setforcephysicalpath(boolean flag)
993 {
994 m_forcephysicalpath = flag;
995 }
996
997 public void setcontentdisposition(string s)
998 {
999 m_contentdisposition = s;
1000 }
1001
1002 public void settotalmaxfilesize(long l)
1003 {
1004 m_totalmaxfilesize = l;
1005 }
1006
1007 public void setmaxfilesize(long l)
1008 {
1009 m_maxfilesize = l;
1010 }
1011
1012 protected string getphysicalpath(string s, int i)
1013 throws ioexception
1014 {
1015 string s1 = new string();
1016 string s2 = new string();
1017 string s3 = new string();
1018 boolean flag = false;
1019 s3 = system.getproperty("file.separator");
1020 if(s == null)
1021 throw new illegalargumentexception("there is no specified destination file (1140).");
1022 if(s.equals(""))
1023 throw new illegalargumentexception("there is no specified destination file (1140).");
1024 if(s.lastindexof("\\") >= 0)
1025 {
1026 s1 = s.substring(0, s.lastindexof("\\"));
1027 s2 = s.substring(s.lastindexof("\\") + 1);
1028 }
1029 if(s.lastindexof("/") >= 0)
1030 {
1031 s1 = s.substring(0, s.lastindexof("/"));
1032 s2 = s.substring(s.lastindexof("/") + 1);
1033 }
1034 s1 = s1.length() != 0 ? s1 : "/";
1035 java.io.file file = new java.io.file(s1);
1036 if(file.exists())
1037 flag = true;
1038 if(i == 0)
1039 {
1040 if(isvirtual(s1))
1041 {
1042 s1 = m_application.getrealpath(s1);
1043 if(s1.endswith(s3))
1044 s1 = s1 + s2;
1045 else
1046 s1 = s1 + s3 + s2;
1047 return s1;
1048 }
1049 if(flag)
1050 {
1051 if(m_denyphysicalpath)
1052 throw new illegalargumentexception("physical path is denied (1125).");
1053 else
1054 return s;
1055 } else
1056 {
1057 throw new illegalargumentexception("this path does not exist (1135).");
1058 }
1059 }
1060 if(i == 1)
1061 {
1062 if(isvirtual(s1))
1063 {
1064 s1 = m_application.getrealpath(s1);
1065 if(s1.endswith(s3))
1066 s1 = s1 + s2;
1067 else
1068 s1 = s1 + s3 + s2;
1069 return s1;
1070 }
1071 if(flag)
1072 throw new illegalargumentexception("the path is not a virtual path.");
1073 else
1074 throw new illegalargumentexception("this path does not exist (1135).");
1075 }
1076 if(i == 2)
1077 {
1078 if(flag)
1079 if(m_denyphysicalpath)
1080 throw new illegalargumentexception("physical path is denied (1125).");
1081 else
1082 return s;
1083 if(isvirtual(s1))
1084 throw new illegalargumentexception("the path is not a physical path.");
1085 else
1086 throw new illegalargumentexception("this path does not exist (1135).");
1087 } else
1088 {
1089 return null;
1090 }
1091 }
1092
1093 public void uploadinfile(string s)
1094 throws ioexception, smartuploadexception
1095 {
1096 int i = 0;
1097 int j = 0;
1098 boolean flag = false;
1099 if(s == null)
1100 throw new illegalargumentexception("there is no specified destination file (1025).");
1101 if(s.length() == 0)
1102 throw new illegalargumentexception("there is no specified destination file (1025).");
1103 if(!isvirtual(s) && m_denyphysicalpath)
1104 throw new securityexception("physical path is denied (1035).");
1105 i = m_request.getcontentlength();
1106 m_binarray = new byte[i];
1107 int k;
1108 for(; j < i; j += k)
1109 try
1110 {
1111 k = m_request.getinputstream().read(m_binarray, j, i - j);
1112 }
1113 catch(exception exception)
1114 {
1115 throw new smartuploadexception("unable to upload.");
1116 }
1117
1118 if(isvirtual(s))
1119 s = m_application.getrealpath(s);
1120 try
1121 {
1122 java.io.file file = new java.io.file(s);
1123 fileoutputstream fileoutputstream = new fileoutputstream(file);
1124 fileoutputstream.write(m_binarray);
1125 fileoutputstream.close();
1126 }
1127 catch(exception exception1)
1128 {
1129 throw new smartuploadexception("the form cannot be saved in the specified file (1030).");
1130 }
1131 }
1132
1133 private boolean isvirtual(string s)
1134 {
1135 if(m_application.getrealpath(s) != null)
1136 {
1137 java.io.file file = new java.io.file(m_application.getrealpath(s));
1138 return file.exists();
1139 } else
1140 {
1141 return false;
1142 }
1143 }
1144 }
1145
1146
1147
1148
1149 jspsmart的使用
1150
1151 上传
1152
1153 // 新建一个smartupload对象
1154 smartupload su = new smartupload();
1155 // 上传初始化
1156 su.initialize(pagecontext);
1157 // 限制每个上传文件的最大长度。
1158 su.setmaxfilesize(10000);
1159 // 限制总上传数据的长度。
1160 su.settotalmaxfilesize(20000);
1161 // 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
1162 su.setallowedfileslist("doc,txt");
1163 // 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
1164 //jsp,htm,html扩展名的文件和没有扩展名的文件。
1165 su.setdeniedfileslist("exe,bat,jsp,htm,html,,");
1166 // 上传文件
1167 su.upload();
1168 // 将上传文件全部保存到指定目录
1169 int count = su.save("/upload");
1170
1171
1172
1173
1174 下载
1175
1176 // 新建一个smartupload对象
1177 smartupload su = new smartupload();
1178 // 初始化
1179 su.initialize(pagecontext);
1180 // 设定contentdisposition为null以禁止浏览器自动打开文件,
1181 //确保点击链接后是下载文件。若不设定,则下载的文件扩展名为
1182 //doc时,浏览器将自动用word打开他。扩展名为pdf时,
1183 //浏览器将用acrobat打开。
1184 su.setcontentdisposition(null);
1185 // 下载文件
1186 su.downloadfile("/upload/test.doc");
1187