1
File.java
2
3 import java.io.ByteArrayInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.math.BigInteger;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import javax.servlet.ServletException;
10
11 // Referenced classes of package com.jspsmart.upload:
12 // SmartUploadException, SmartUpload
13
14 public class File
15 {
16
17 private SmartUpload m_parent;
18 private int m_startData;
19 private int m_endData;
20 private int m_size;
21 private String m_fieldname;
22 private String m_filename;
23 private String m_fileExt;
24 private String m_filePathName;
25 private String m_contentType;
26 private String m_contentDisp;
27 private String m_typeMime;
28 private String m_subTypeMime;
29 private String m_contentString;
30 private boolean m_isMissing;
31 public static final int SAVEAS_AUTO = 0 ;
32 public static final int SAVEAS_VIRTUAL = 1 ;
33 public static final int SAVEAS_PHYSICAL = 2 ;
34
35 File()
36 {
37 m_startData = 0 ;
38 m_endData = 0 ;
39 m_size = 0 ;
40 m_fieldname = new String();
41 m_filename = new String();
42 m_fileExt = new String();
43 m_filePathName = new String();
44 m_contentType = new String();
45 m_contentDisp = new String();
46 m_typeMime = new String();
47 m_subTypeMime = new String();
48 m_contentString = new String();
49 m_isMissing = true ;
50 }
51
52 public void saveAs(String s)
53 throws IOException, SmartUploadException
54 {
55 saveAs(s, 0 );
56 }
57
58 public void saveAs(String s, int i)
59 throws IOException, SmartUploadException
60 {
61 String s1 = new String();
62 s1 = m_parent.getPhysicalPath(s, i);
63 if (s1 == null )
64 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
65 try
66 {
67 java.io.File file = new java.io.File(s1);
68 FileOutputStream fileoutputstream = new FileOutputStream(file);
69 fileoutputstream.write(m_parent.m_binArray, m_startData, m_size);
70 fileoutputstream.close();
71 }
72 catch (IOException ioexception)
73 {
74 throw new SmartUploadException( " File can't be saved (1120). " );
75 }
76 }
77
78 public void fileToField(ResultSet resultset, String s)
79 throws ServletException, IOException, SmartUploadException, SQLException
80 {
81 long l = 0L ;
82 int i = 0x10000 ;
83 int j = 0 ;
84 int k = m_startData;
85 if (resultset == null )
86 throw new IllegalArgumentException( " The RecordSet cannot be null (1145). " );
87 if (s == null )
88 throw new IllegalArgumentException( " The columnName cannot be null (1150). " );
89 if (s.length() == 0 )
90 throw new IllegalArgumentException( " The columnName cannot be empty (1155). " );
91 l = BigInteger.valueOf(m_size).divide(BigInteger.valueOf(i)).longValue();
92 j = BigInteger.valueOf(m_size).mod(BigInteger.valueOf(i)).intValue();
93 try
94 {
95 for ( int i1 = 1 ; ( long )i1 < l; i1 ++ )
96 {
97 resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, i), i);
98 k = k != 0 ? k : 1 ;
99 k = i1 * i + m_startData;
100 }
101
102 if (j > 0 )
103 resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, j), j);
104 }
105 catch (SQLException sqlexception)
106 {
107 byte abyte0[] = new byte [m_size];
108 System.arraycopy(m_parent.m_binArray, m_startData, abyte0, 0 , m_size);
109 resultset.updateBytes(s, abyte0);
110 }
111 catch (Exception exception)
112 {
113 throw new SmartUploadException( " Unable to save file in the DataBase (1130). " );
114 }
115 }
116
117 public boolean isMissing()
118 {
119 return m_isMissing;
120 }
121
122 public String getFieldName()
123 {
124 return m_fieldname;
125 }
126
127 public String getFileName()
128 {
129 return m_filename;
130 }
131
132 public String getFilePathName()
133 {
134 return m_filePathName;
135 }
136
137 public String getFileExt()
138 {
139 return m_fileExt;
140 }
141
142 public String getContentType()
143 {
144 return m_contentType;
145 }
146
147 public String getContentDisp()
148 {
149 return m_contentDisp;
150 }
151
152 public String getContentString()
153 {
154 String s = new String(m_parent.m_binArray, m_startData, m_size);
155 return s;
156 }
157
158 public String getTypeMIME()
159 throws IOException
160 {
161 return m_typeMime;
162 }
163
164 public String getSubTypeMIME()
165 {
166 return m_subTypeMime;
167 }
168
169 public int getSize()
170 {
171 return m_size;
172 }
173
174 protected int getStartData()
175 {
176 return m_startData;
177 }
178
179 protected int getEndData()
180 {
181 return m_endData;
182 }
183
184 protected void setParent(SmartUpload smartupload)
185 {
186 m_parent = smartupload;
187 }
188
189 protected void setStartData( int i)
190 {
191 m_startData = i;
192 }
193
194 protected void setEndData( int i)
195 {
196 m_endData = i;
197 }
198
199 protected void setSize( int i)
200 {
201 m_size = i;
202 }
203
204 protected void setIsMissing( boolean flag)
205 {
206 m_isMissing = flag;
207 }
208
209 protected void setFieldName(String s)
210 {
211 m_fieldname = s;
212 }
213
214 protected void setFileName(String s)
215 {
216 m_filename = s;
217 }
218
219 protected void setFilePathName(String s)
220 {
221 m_filePathName = s;
222 }
223
224 protected void setFileExt(String s)
225 {
226 m_fileExt = s;
227 }
228
229 protected void setContentType(String s)
230 {
231 m_contentType = s;
232 }
233
234 protected void setContentDisp(String s)
235 {
236 m_contentDisp = s;
237 }
238
239 protected void setTypeMIME(String s)
240 {
241 m_typeMime = s;
242 }
243
244 protected void setSubTypeMIME(String s)
245 {
246 m_subTypeMime = s;
247 }
248
249 public byte getBinaryData( int i)
250 {
251 if (m_startData + i > m_endData)
252 throw new ArrayIndexOutOfBoundsException( " Index Out of range (1115). " );
253 if (m_startData + i <= m_endData)
254 return m_parent.m_binArray[m_startData + i];
255 else
256 return 0 ;
257 }
258 }
259
260
261 Files.java
262
263 import java.io.IOException;
264 import java.util. * ;
265
266 // Referenced classes of package com.jspsmart.upload:
267 // File, SmartUpload
268
269 public class Files
270 {
271
272 private SmartUpload m_parent;
273 private Hashtable m_files;
274 private int m_counter;
275
276 Files()
277 {
278 m_files = new Hashtable();
279 m_counter = 0 ;
280 }
281
282 protected void addFile(File file)
283 {
284 if (file == null )
285 {
286 throw new IllegalArgumentException( " newFile cannot be null. " );
287 } else
288 {
289 m_files.put( new Integer(m_counter), file);
290 m_counter ++ ;
291 return ;
292 }
293 }
294
295 public File getFile( int i)
296 {
297 if (i < 0 )
298 throw new IllegalArgumentException( " File's index cannot be a negative value (1210). " );
299 File file = (File)m_files.get( new Integer(i));
300 if (file == null )
301 throw new IllegalArgumentException( " Files' name is invalid or does not exist (1205). " );
302 else
303 return file;
304 }
305
306 public int getCount()
307 {
308 return m_counter;
309 }
310
311 public long getSize()
312 throws IOException
313 {
314 long l = 0L ;
315 for ( int i = 0 ; i < m_counter; i ++ )
316 l += getFile(i).getSize();
317
318 return l;
319 }
320
321 public Collection getCollection()
322 {
323 return m_files.values();
324 }
325
326 public Enumeration getEnumeration()
327 {
328 return m_files.elements();
329 }
330 }
331
332
333 Request.java
334
335 import java.util.Enumeration;
336 import java.util.Hashtable;
337
338 public class Request
339 {
340
341 private Hashtable m_parameters;
342 private int m_counter;
343
344 Request()
345 {
346 m_parameters = new Hashtable();
347 m_counter = 0 ;
348 }
349
350 protected void putParameter(String s, String s1)
351 {
352 if (s == null )
353 throw new IllegalArgumentException( " The name of an element cannot be null. " );
354 if (m_parameters.containsKey(s))
355 {
356 Hashtable hashtable = (Hashtable)m_parameters.get(s);
357 hashtable.put( new Integer(hashtable.size()), s1);
358 } else
359 {
360 Hashtable hashtable1 = new Hashtable();
361 hashtable1.put( new Integer( 0 ), s1);
362 m_parameters.put(s, hashtable1);
363 m_counter ++ ;
364 }
365 }
366
367 public String getParameter(String s)
368 {
369 if (s == null )
370 throw new IllegalArgumentException( " Form's name is invalid or does not exist (1305). " );
371 Hashtable hashtable = (Hashtable)m_parameters.get(s);
372 if (hashtable == null )
373 return null ;
374 else
375 return (String)hashtable.get( new Integer( 0 ));
376 }
377
378 public Enumeration getParameterNames()
379 {
380 return m_parameters.keys();
381 }
382
383 public String[] getParameterValues(String s)
384 {
385 if (s == null )
386 throw new IllegalArgumentException( " Form's name is invalid or does not exist (1305). " );
387 Hashtable hashtable = (Hashtable)m_parameters.get(s);
388 if (hashtable == null )
389 return null ;
390 String as[] = new String[hashtable.size()];
391 for ( int i = 0 ; i < hashtable.size(); i ++ )
392 as[i] = (String)hashtable.get( new Integer(i));
393
394 return as;
395 }
396 }
397
398
399 SmartUploadException .java
400
401 public class SmartUploadException extends Exception
402 {
403
404 SmartUploadException(String s)
405 {
406 super (s);
407 }
408 }
409
410
411 SmartUpload.java
412
413 import java.io. * ;
414 import java.sql.ResultSet;
415 import java.sql.SQLException;
416 import java.util.Vector;
417 import javax.servlet. * ;
418 import javax.servlet.http. * ;
419 import javax.servlet.jsp.JspWriter;
420 import javax.servlet.jsp.PageContext;
421
422 // Referenced classes of package com.jspsmart.upload:
423 // Files, Request, SmartUploadException, File
424
425 public class SmartUpload
426 {
427
428 protected byte m_binArray[];
429 protected HttpServletRequest m_request;
430 protected HttpServletResponse m_response;
431 protected ServletContext m_application;
432 private int m_totalBytes;
433 private int m_currentIndex;
434 private int m_startData;
435 private int m_endData;
436 private String m_boundary;
437 private long m_totalMaxFileSize;
438 private long m_maxFileSize;
439 private Vector m_deniedFilesList;
440 private Vector m_allowedFilesList;
441 private boolean m_denyPhysicalPath;
442 private boolean m_forcePhysicalPath;
443 private String m_contentDisposition;
444 public static final int SAVE_AUTO = 0 ;
445 public static final int SAVE_VIRTUAL = 1 ;
446 public static final int SAVE_PHYSICAL = 2 ;
447 private Files m_files;
448 private Request m_formRequest;
449
450 public SmartUpload()
451 {
452 m_totalBytes = 0 ;
453 m_currentIndex = 0 ;
454 m_startData = 0 ;
455 m_endData = 0 ;
456 m_boundary = new String();
457 m_totalMaxFileSize = 0L ;
458 m_maxFileSize = 0L ;
459 m_deniedFilesList = new Vector();
460 m_allowedFilesList = new Vector();
461 m_denyPhysicalPath = false ;
462 m_forcePhysicalPath = false ;
463 m_contentDisposition = new String();
464 m_files = new Files();
465 m_formRequest = new Request();
466 }
467
468 public final void init(ServletConfig servletconfig)
469 throws ServletException
470 {
471 m_application = servletconfig.getServletContext();
472 }
473
474 public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
475 throws ServletException, IOException
476 {
477 m_request = httpservletrequest;
478 m_response = httpservletresponse;
479 }
480
481 public final void initialize(ServletConfig servletconfig, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
482 throws ServletException
483 {
484 m_application = servletconfig.getServletContext();
485 m_request = httpservletrequest;
486 m_response = httpservletresponse;
487 }
488
489 public final void initialize(PageContext pagecontext)
490 throws ServletException
491 {
492 m_application = pagecontext.getServletContext();
493 m_request = (HttpServletRequest)pagecontext.getRequest();
494 m_response = (HttpServletResponse)pagecontext.getResponse();
495 }
496
497 public final void initialize(ServletContext servletcontext, HttpSession httpsession, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, JspWriter jspwriter)
498 throws ServletException
499 {
500 m_application = servletcontext;
501 m_request = httpservletrequest;
502 m_response = httpservletresponse;
503 }
504
505 public void upload()
506 throws ServletException, IOException, SmartUploadException
507 {
508 int i = 0 ;
509 boolean flag = false ;
510 long l = 0L ;
511 boolean flag1 = false ;
512 String s = new String();
513 String s2 = new String();
514 String s4 = new String();
515 String s5 = new String();
516 String s6 = new String();
517 String s7 = new String();
518 String s8 = new String();
519 String s9 = new String();
520 String s10 = new String();
521 boolean flag2 = false ;
522 m_totalBytes = m_request.getContentLength();
523 m_binArray = new byte [m_totalBytes];
524 int j;
525 for (; i < m_totalBytes; i += j)
526 try
527 {
528 m_request.getInputStream();
529 j = m_request.getInputStream().read(m_binArray, i, m_totalBytes - i);
530 }
531 catch (Exception exception)
532 {
533 throw new SmartUploadException( " Unable to upload. " );
534 }
535
536 for (; ! flag1 && m_currentIndex < m_totalBytes; m_currentIndex ++ )
537 if (m_binArray[m_currentIndex] == 13 )
538 flag1 = true ;
539 else
540 m_boundary = m_boundary + ( char )m_binArray[m_currentIndex];
541
542 if (m_currentIndex == 1 )
543 return ;
544 for (m_currentIndex ++ ; m_currentIndex < m_totalBytes; m_currentIndex = m_currentIndex + 2 )
545 {
546 String s1 = getDataHeader();
547 m_currentIndex = m_currentIndex + 2 ;
548 boolean flag3 = s1.indexOf( " filename " ) > 0 ;
549 String s3 = getDataFieldValue(s1, " name " );
550 if (flag3)
551 {
552 s6 = getDataFieldValue(s1, " filename " );
553 s4 = getFileName(s6);
554 s5 = getFileExt(s4);
555 s7 = getContentType(s1);
556 s8 = getContentDisp(s1);
557 s9 = getTypeMIME(s7);
558 s10 = getSubTypeMIME(s7);
559 }
560 getDataSection();
561 if (flag3 && s4.length() > 0 )
562 {
563 if (m_deniedFilesList.contains(s5))
564 throw new SecurityException( " The extension of the file is denied to be uploaded (1015). " );
565 if ( ! m_allowedFilesList.isEmpty() && ! m_allowedFilesList.contains(s5))
566 throw new SecurityException( " The extension of the file is not allowed to be uploaded (1010). " );
567 if (m_maxFileSize > 0L && ( long )((m_endData - m_startData) + 1 ) > m_maxFileSize)
568 throw new SecurityException( " Size exceeded for this file : " + s4 + " (1105). " );
569 l += (m_endData - m_startData) + 1 ;
570 if (m_totalMaxFileSize > 0L && l > m_totalMaxFileSize)
571 throw new SecurityException( " Total File Size exceeded (1110). " );
572 }
573 if (flag3)
574 {
575 com.facet.jspsmart.upload.File file = new com.facet.jspsmart.upload.File();
576 file.setParent( this );
577 file.setFieldName(s3);
578 file.setFileName(s4);
579 file.setFileExt(s5);
580 file.setFilePathName(s6);
581 file.setIsMissing(s6.length() == 0 );
582 file.setContentType(s7);
583 file.setContentDisp(s8);
584 file.setTypeMIME(s9);
585 file.setSubTypeMIME(s10);
586 if (s7.indexOf( " application/x-macbinary " ) > 0 )
587 m_startData = m_startData + 128 ;
588 file.setSize((m_endData - m_startData) + 1 );
589 file.setStartData(m_startData);
590 file.setEndData(m_endData);
591 m_files.addFile(file);
592 } else
593 {
594 String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1 );
595 m_formRequest.putParameter(s3, s11);
596 }
597 if (( char )m_binArray[m_currentIndex + 1 ] == ' - ' )
598 break ;
599 }
600
601 }
602
603 public int save(String s)
604 throws ServletException, IOException, SmartUploadException
605 {
606 return save(s, 0 );
607 }
608
609 public int save(String s, int i)
610 throws ServletException, IOException, SmartUploadException
611 {
612 int j = 0 ;
613 if (s == null )
614 s = m_application.getRealPath( " / " );
615 if (s.indexOf( " / " ) != - 1 )
616 {
617 if (s.charAt(s.length() - 1 ) != ' / ' )
618 s = s + " / " ;
619 } else
620 if (s.charAt(s.length() - 1 ) != ' \\ ' )
621 s = s + " \\ " ;
622 for ( int k = 0 ; k < m_files.getCount(); k ++ )
623 if ( ! m_files.getFile(k).isMissing())
624 {
625 m_files.getFile(k).saveAs(s + m_files.getFile(k).getFileName(), i);
626 j ++ ;
627 }
628
629 return j;
630 }
631
632 public int getSize()
633 {
634 return m_totalBytes;
635 }
636
637 public byte getBinaryData( int i)
638 {
639 byte byte0;
640 try
641 {
642 byte0 = m_binArray[i];
643 }
644 catch (Exception exception)
645 {
646 throw new ArrayIndexOutOfBoundsException( " Index out of range (1005). " );
647 }
648 return byte0;
649 }
650
651 public Files getFiles()
652 {
653 return m_files;
654 }
655
656 public Request getRequest()
657 {
658 return m_formRequest;
659 }
660
661 public void downloadFile(String s)
662 throws ServletException, IOException, SmartUploadException
663 {
664 downloadFile(s, null , null );
665 }
666
667 public void downloadFile(String s, String s1)
668 throws ServletException, IOException, SmartUploadException, SmartUploadException
669 {
670 downloadFile(s, s1, null );
671 }
672
673 public void downloadFile(String s, String s1, String s2)
674 throws ServletException, IOException, SmartUploadException
675 {
676 downloadFile(s, s1, s2, 65000 );
677 }
678
679 public void downloadFile(String s, String s1, String s2, int i)
680 throws ServletException, IOException, SmartUploadException
681 {
682 if (s == null )
683 throw new IllegalArgumentException( " File ' " + s + " ' not found (1040). " );
684 if (s.equals( "" ))
685 throw new IllegalArgumentException( " File ' " + s + " ' not found (1040). " );
686 if ( ! isVirtual(s) && m_denyPhysicalPath)
687 throw new SecurityException( " Physical path is denied (1035). " );
688 if (isVirtual(s))
689 s = m_application.getRealPath(s);
690 java.io.File file = new java.io.File(s);
691 FileInputStream fileinputstream = new FileInputStream(file);
692 long l = file.length();
693 boolean flag = false ;
694 int k = 0 ;
695 byte abyte0[] = new byte [i];
696 if (s1 == null )
697 m_response.setContentType( " application/x-msdownload " );
698 else
699 if (s1.length() == 0 )
700 m_response.setContentType( " application/x-msdownload " );
701 else
702 m_response.setContentType(s1);
703 m_response.setContentLength(( int )l);
704 m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : " attachment; " ;
705 if (s2 == null )
706 m_response.setHeader( " Content-Disposition " , m_contentDisposition + " filename= " + getFileName(s));
707 else
708 if (s2.length() == 0 )
709 m_response.setHeader( " Content-Disposition " , m_contentDisposition);
710 else
711 m_response.setHeader( " Content-Disposition " , m_contentDisposition + " filename= " + s2);
712 while (( long )k < l)
713 {
714 int j = fileinputstream.read(abyte0, 0 , i);
715 k += j;
716 m_response.getOutputStream().write(abyte0, 0 , j);
717 }
718 fileinputstream.close();
719 }
720
721 public void downloadField(ResultSet resultset, String s, String s1, String s2)
722 throws ServletException, IOException, SQLException
723 {
724 if (resultset == null )
725 throw new IllegalArgumentException( " The RecordSet cannot be null (1045). " );
726 if (s == null )
727 throw new IllegalArgumentException( " The columnName cannot be null (1050). " );
728 if (s.length() == 0 )
729 throw new IllegalArgumentException( " The columnName cannot be empty (1055). " );
730 byte abyte0[] = resultset.getBytes(s);
731 if (s1 == null )
732 m_response.setContentType( " application/x-msdownload " );
733 else
734 if (s1.length() == 0 )
735 m_response.setContentType( " application/x-msdownload " );
736 else
737 m_response.setContentType(s1);
738 m_response.setContentLength(abyte0.length);
739 if (s2 == null )
740 m_response.setHeader( " Content-Disposition " , " attachment; " );
741 else
742 if (s2.length() == 0 )
743 m_response.setHeader( " Content-Disposition " , " attachment; " );
744 else
745 m_response.setHeader( " Content-Disposition " , " attachment; filename= " + s2);
746 m_response.getOutputStream().write(abyte0, 0 , abyte0.length);
747 }
748
749 public void fieldToFile(ResultSet resultset, String s, String s1)
750 throws ServletException, IOException, SmartUploadException, SQLException
751 {
752 try
753 {
754 if (m_application.getRealPath(s1) != null )
755 s1 = m_application.getRealPath(s1);
756 InputStream inputstream = resultset.getBinaryStream(s);
757 FileOutputStream fileoutputstream = new FileOutputStream(s1);
758 int i;
759 while ((i = inputstream.read()) != - 1 )
760 fileoutputstream.write(i);
761 fileoutputstream.close();
762 }
763 catch (Exception exception)
764 {
765 throw new SmartUploadException( " Unable to save file from the DataBase (1020). " );
766 }
767 }
768
769 private String getDataFieldValue(String s, String s1)
770 {
771 String s2 = new String();
772 String s3 = new String();
773 int i = 0 ;
774 boolean flag = false ;
775 boolean flag1 = false ;
776 boolean flag2 = false ;
777 s2 = s1 + " = " + ' " ' ;
778 i = s.indexOf(s2);
779 if (i > 0 )
780 {
781 int j = i + s2.length();
782 int k = j;
783 s2 = " \ "" ;
784 int l = s.indexOf(s2, j);
785 if (k > 0 && l > 0 )
786 s3 = s.substring(k, l);
787 }
788 return s3;
789 }
790
791 private String getFileExt(String s)
792 {
793 String s1 = new String();
794 int i = 0 ;
795 int j = 0 ;
796 if (s == null )
797 return null ;
798 i = s.lastIndexOf( 46 ) + 1 ;
799 j = s.length();
800 s1 = s.substring(i, j);
801 if (s.lastIndexOf( 46 ) > 0 )
802 return s1;
803 else
804 return "" ;
805 }
806
807 private String getContentType(String s)
808 {
809 String s1 = new String();
810 String s2 = new String();
811 int i = 0 ;
812 boolean flag = false ;
813 s1 = " Content-Type: " ;
814 i = s.indexOf(s1) + s1.length();
815 if (i != - 1 )
816 {
817 int j = s.length();
818 s2 = s.substring(i, j);
819 }
820 return s2;
821 }
822
823 private String getTypeMIME(String s)
824 {
825 String s1 = new String();
826 int i = 0 ;
827 i = s.indexOf( " / " );
828 if (i != - 1 )
829 return s.substring( 1 , i);
830 else
831 return s;
832 }
833
834 private String getSubTypeMIME(String s)
835 {
836 String s1 = new String();
837 int i = 0 ;
838 boolean flag = false ;
839 i = s.indexOf( " / " ) + 1 ;
840 if (i != - 1 )
841 {
842 int j = s.length();
843 return s.substring(i, j);
844 } else
845 {
846 return s;
847 }
848 }
849
850 private String getContentDisp(String s)
851 {
852 String s1 = new String();
853 int i = 0 ;
854 int j = 0 ;
855 i = s.indexOf( " : " ) + 1 ;
856 j = s.indexOf( " ; " );
857 s1 = s.substring(i, j);
858 return s1;
859 }
860
861 private void getDataSection()
862 {
863 boolean flag = false ;
864 String s = new String();
865 int i = m_currentIndex;
866 int j = 0 ;
867 int k = m_boundary.length();
868 m_startData = m_currentIndex;
869 m_endData = 0 ;
870 while (i < m_totalBytes)
871 if (m_binArray[i] == ( byte )m_boundary.charAt(j))
872 {
873 if (j == k - 1 )
874 {
875 m_endData = ((i - k) + 1 ) - 3 ;
876 break ;
877 }
878 i ++ ;
879 j ++ ;
880 } else
881 {
882 i ++ ;
883 j = 0 ;
884 }
885 m_currentIndex = m_endData + k + 3 ;
886 }
887
888 private String getDataHeader()
889 {
890 int i = m_currentIndex;
891 int j = 0 ;
892 boolean flag = false ;
893 for ( boolean flag1 = false ; ! flag1;)
894 if (m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2 ] == 13 )
895 {
896 flag1 = true ;
897 j = m_currentIndex - 1 ;
898 m_currentIndex = m_currentIndex + 2 ;
899 } else
900 {
901 m_currentIndex ++ ;
902 }
903
904 String s = new String(m_binArray, i, (j - i) + 1 );
905 return s;
906 }
907
908 private String getFileName(String s)
909 {
910 String s1 = new String();
911 String s2 = new String();
912 int i = 0 ;
913 boolean flag = false ;
914 boolean flag1 = false ;
915 boolean flag2 = false ;
916 i = s.lastIndexOf( 47 );
917 if (i != - 1 )
918 return s.substring(i + 1 , s.length());
919 i = s.lastIndexOf( 92 );
920 if (i != - 1 )
921 return s.substring(i + 1 , s.length());
922 else
923 return s;
924 }
925
926 public void setDeniedFilesList(String s)
927 throws ServletException, IOException, SQLException
928 {
929 String s1 = "" ;
930 if (s != null )
931 {
932 String s2 = "" ;
933 for ( int i = 0 ; i < s.length(); i ++ )
934 if (s.charAt(i) == ' , ' )
935 {
936 if ( ! m_deniedFilesList.contains(s2))
937 m_deniedFilesList.addElement(s2);
938 s2 = "" ;
939 } else
940 {
941 s2 = s2 + s.charAt(i);
942 }
943
944 if (s2 != "" )
945 m_deniedFilesList.addElement(s2);
946 } else
947 {
948 m_deniedFilesList = null ;
949 }
950 }
951
952 public void setAllowedFilesList(String s)
953 {
954 String s1 = "" ;
955 if (s != null )
956 {
957 String s2 = "" ;
958 for ( int i = 0 ; i < s.length(); i ++ )
959 if (s.charAt(i) == ' , ' )
960 {
961 if ( ! m_allowedFilesList.contains(s2))
962 m_allowedFilesList.addElement(s2);
963 s2 = "" ;
964 } else
965 {
966 s2 = s2 + s.charAt(i);
967 }
968
969 if (s2 != "" )
970 m_allowedFilesList.addElement(s2);
971 } else
972 {
973 m_allowedFilesList = null ;
974 }
975 }
976
977 public void setDenyPhysicalPath( boolean flag)
978 {
979 m_denyPhysicalPath = flag;
980 }
981
982 public void setForcePhysicalPath( boolean flag)
983 {
984 m_forcePhysicalPath = flag;
985 }
986
987 public void setContentDisposition(String s)
988 {
989 m_contentDisposition = s;
990 }
991
992 public void setTotalMaxFileSize( long l)
993 {
994 m_totalMaxFileSize = l;
995 }
996
997 public void setMaxFileSize( long l)
998 {
999 m_maxFileSize = l;
1000 }
1001
1002 protected String getPhysicalPath(String s, int i)
1003 throws IOException
1004 {
1005 String s1 = new String();
1006 String s2 = new String();
1007 String s3 = new String();
1008 boolean flag = false ;
1009 s3 = System.getProperty( " file.separator " );
1010 if (s == null )
1011 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
1012 if (s.equals( "" ))
1013 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
1014 if (s.lastIndexOf( " \\ " ) >= 0 )
1015 {
1016 s1 = s.substring( 0 , s.lastIndexOf( " \\ " ));
1017 s2 = s.substring(s.lastIndexOf( " \\ " ) + 1 );
1018 }
1019 if (s.lastIndexOf( " / " ) >= 0 )
1020 {
1021 s1 = s.substring( 0 , s.lastIndexOf( " / " ));
1022 s2 = s.substring(s.lastIndexOf( " / " ) + 1 );
1023 }
1024 s1 = s1.length() != 0 ? s1 : " / " ;
1025 java.io.File file = new java.io.File(s1);
1026 if (file.exists())
1027 flag = true ;
1028 if (i == 0 )
1029 {
1030 if (isVirtual(s1))
1031 {
1032 s1 = m_application.getRealPath(s1);
1033 if (s1.endsWith(s3))
1034 s1 = s1 + s2;
1035 else
1036 s1 = s1 + s3 + s2;
1037 return s1;
1038 }
1039 if (flag)
1040 {
1041 if (m_denyPhysicalPath)
1042 throw new IllegalArgumentException( " Physical path is denied (1125). " );
1043 else
1044 return s;
1045 } else
1046 {
1047 throw new IllegalArgumentException( " This path does not exist (1135). " );
1048 }
1049 }
1050 if (i == 1 )
1051 {
1052 if (isVirtual(s1))
1053 {
1054 s1 = m_application.getRealPath(s1);
1055 if (s1.endsWith(s3))
1056 s1 = s1 + s2;
1057 else
1058 s1 = s1 + s3 + s2;
1059 return s1;
1060 }
1061 if (flag)
1062 throw new IllegalArgumentException( " The path is not a virtual path. " );
1063 else
1064 throw new IllegalArgumentException( " This path does not exist (1135). " );
1065 }
1066 if (i == 2 )
1067 {
1068 if (flag)
1069 if (m_denyPhysicalPath)
1070 throw new IllegalArgumentException( " Physical path is denied (1125). " );
1071 else
1072 return s;
1073 if (isVirtual(s1))
1074 throw new IllegalArgumentException( " The path is not a physical path. " );
1075 else
1076 throw new IllegalArgumentException( " This path does not exist (1135). " );
1077 } else
1078 {
1079 return null ;
1080 }
1081 }
1082
1083 public void uploadInFile(String s)
1084 throws IOException, SmartUploadException
1085 {
1086 int i = 0 ;
1087 int j = 0 ;
1088 boolean flag = false ;
1089 if (s == null )
1090 throw new IllegalArgumentException( " There is no specified destination file (1025). " );
1091 if (s.length() == 0 )
1092 throw new IllegalArgumentException( " There is no specified destination file (1025). " );
1093 if ( ! isVirtual(s) && m_denyPhysicalPath)
1094 throw new SecurityException( " Physical path is denied (1035). " );
1095 i = m_request.getContentLength();
1096 m_binArray = new byte [i];
1097 int k;
1098 for (; j < i; j += k)
1099 try
1100 {
1101 k = m_request.getInputStream().read(m_binArray, j, i - j);
1102 }
1103 catch (Exception exception)
1104 {
1105 throw new SmartUploadException( " Unable to upload. " );
1106 }
1107
1108 if (isVirtual(s))
1109 s = m_application.getRealPath(s);
1110 try
1111 {
1112 java.io.File file = new java.io.File(s);
1113 FileOutputStream fileoutputstream = new FileOutputStream(file);
1114 fileoutputstream.write(m_binArray);
1115 fileoutputstream.close();
1116 }
1117 catch (Exception exception1)
1118 {
1119 throw new SmartUploadException( " The Form cannot be saved in the specified file (1030). " );
1120 }
1121 }
1122
1123 private boolean isVirtual(String s)
1124 {
1125 if (m_application.getRealPath(s) != null )
1126 {
1127 java.io.File file = new java.io.File(m_application.getRealPath(s));
1128 return file.exists();
1129 } else
1130 {
1131 return false ;
1132 }
1133 }
1134 }
2
3 import java.io.ByteArrayInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.math.BigInteger;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import javax.servlet.ServletException;
10
11 // Referenced classes of package com.jspsmart.upload:
12 // SmartUploadException, SmartUpload
13
14 public class File
15 {
16
17 private SmartUpload m_parent;
18 private int m_startData;
19 private int m_endData;
20 private int m_size;
21 private String m_fieldname;
22 private String m_filename;
23 private String m_fileExt;
24 private String m_filePathName;
25 private String m_contentType;
26 private String m_contentDisp;
27 private String m_typeMime;
28 private String m_subTypeMime;
29 private String m_contentString;
30 private boolean m_isMissing;
31 public static final int SAVEAS_AUTO = 0 ;
32 public static final int SAVEAS_VIRTUAL = 1 ;
33 public static final int SAVEAS_PHYSICAL = 2 ;
34
35 File()
36 {
37 m_startData = 0 ;
38 m_endData = 0 ;
39 m_size = 0 ;
40 m_fieldname = new String();
41 m_filename = new String();
42 m_fileExt = new String();
43 m_filePathName = new String();
44 m_contentType = new String();
45 m_contentDisp = new String();
46 m_typeMime = new String();
47 m_subTypeMime = new String();
48 m_contentString = new String();
49 m_isMissing = true ;
50 }
51
52 public void saveAs(String s)
53 throws IOException, SmartUploadException
54 {
55 saveAs(s, 0 );
56 }
57
58 public void saveAs(String s, int i)
59 throws IOException, SmartUploadException
60 {
61 String s1 = new String();
62 s1 = m_parent.getPhysicalPath(s, i);
63 if (s1 == null )
64 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
65 try
66 {
67 java.io.File file = new java.io.File(s1);
68 FileOutputStream fileoutputstream = new FileOutputStream(file);
69 fileoutputstream.write(m_parent.m_binArray, m_startData, m_size);
70 fileoutputstream.close();
71 }
72 catch (IOException ioexception)
73 {
74 throw new SmartUploadException( " File can't be saved (1120). " );
75 }
76 }
77
78 public void fileToField(ResultSet resultset, String s)
79 throws ServletException, IOException, SmartUploadException, SQLException
80 {
81 long l = 0L ;
82 int i = 0x10000 ;
83 int j = 0 ;
84 int k = m_startData;
85 if (resultset == null )
86 throw new IllegalArgumentException( " The RecordSet cannot be null (1145). " );
87 if (s == null )
88 throw new IllegalArgumentException( " The columnName cannot be null (1150). " );
89 if (s.length() == 0 )
90 throw new IllegalArgumentException( " The columnName cannot be empty (1155). " );
91 l = BigInteger.valueOf(m_size).divide(BigInteger.valueOf(i)).longValue();
92 j = BigInteger.valueOf(m_size).mod(BigInteger.valueOf(i)).intValue();
93 try
94 {
95 for ( int i1 = 1 ; ( long )i1 < l; i1 ++ )
96 {
97 resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, i), i);
98 k = k != 0 ? k : 1 ;
99 k = i1 * i + m_startData;
100 }
101
102 if (j > 0 )
103 resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, j), j);
104 }
105 catch (SQLException sqlexception)
106 {
107 byte abyte0[] = new byte [m_size];
108 System.arraycopy(m_parent.m_binArray, m_startData, abyte0, 0 , m_size);
109 resultset.updateBytes(s, abyte0);
110 }
111 catch (Exception exception)
112 {
113 throw new SmartUploadException( " Unable to save file in the DataBase (1130). " );
114 }
115 }
116
117 public boolean isMissing()
118 {
119 return m_isMissing;
120 }
121
122 public String getFieldName()
123 {
124 return m_fieldname;
125 }
126
127 public String getFileName()
128 {
129 return m_filename;
130 }
131
132 public String getFilePathName()
133 {
134 return m_filePathName;
135 }
136
137 public String getFileExt()
138 {
139 return m_fileExt;
140 }
141
142 public String getContentType()
143 {
144 return m_contentType;
145 }
146
147 public String getContentDisp()
148 {
149 return m_contentDisp;
150 }
151
152 public String getContentString()
153 {
154 String s = new String(m_parent.m_binArray, m_startData, m_size);
155 return s;
156 }
157
158 public String getTypeMIME()
159 throws IOException
160 {
161 return m_typeMime;
162 }
163
164 public String getSubTypeMIME()
165 {
166 return m_subTypeMime;
167 }
168
169 public int getSize()
170 {
171 return m_size;
172 }
173
174 protected int getStartData()
175 {
176 return m_startData;
177 }
178
179 protected int getEndData()
180 {
181 return m_endData;
182 }
183
184 protected void setParent(SmartUpload smartupload)
185 {
186 m_parent = smartupload;
187 }
188
189 protected void setStartData( int i)
190 {
191 m_startData = i;
192 }
193
194 protected void setEndData( int i)
195 {
196 m_endData = i;
197 }
198
199 protected void setSize( int i)
200 {
201 m_size = i;
202 }
203
204 protected void setIsMissing( boolean flag)
205 {
206 m_isMissing = flag;
207 }
208
209 protected void setFieldName(String s)
210 {
211 m_fieldname = s;
212 }
213
214 protected void setFileName(String s)
215 {
216 m_filename = s;
217 }
218
219 protected void setFilePathName(String s)
220 {
221 m_filePathName = s;
222 }
223
224 protected void setFileExt(String s)
225 {
226 m_fileExt = s;
227 }
228
229 protected void setContentType(String s)
230 {
231 m_contentType = s;
232 }
233
234 protected void setContentDisp(String s)
235 {
236 m_contentDisp = s;
237 }
238
239 protected void setTypeMIME(String s)
240 {
241 m_typeMime = s;
242 }
243
244 protected void setSubTypeMIME(String s)
245 {
246 m_subTypeMime = s;
247 }
248
249 public byte getBinaryData( int i)
250 {
251 if (m_startData + i > m_endData)
252 throw new ArrayIndexOutOfBoundsException( " Index Out of range (1115). " );
253 if (m_startData + i <= m_endData)
254 return m_parent.m_binArray[m_startData + i];
255 else
256 return 0 ;
257 }
258 }
259
260
261 Files.java
262
263 import java.io.IOException;
264 import java.util. * ;
265
266 // Referenced classes of package com.jspsmart.upload:
267 // File, SmartUpload
268
269 public class Files
270 {
271
272 private SmartUpload m_parent;
273 private Hashtable m_files;
274 private int m_counter;
275
276 Files()
277 {
278 m_files = new Hashtable();
279 m_counter = 0 ;
280 }
281
282 protected void addFile(File file)
283 {
284 if (file == null )
285 {
286 throw new IllegalArgumentException( " newFile cannot be null. " );
287 } else
288 {
289 m_files.put( new Integer(m_counter), file);
290 m_counter ++ ;
291 return ;
292 }
293 }
294
295 public File getFile( int i)
296 {
297 if (i < 0 )
298 throw new IllegalArgumentException( " File's index cannot be a negative value (1210). " );
299 File file = (File)m_files.get( new Integer(i));
300 if (file == null )
301 throw new IllegalArgumentException( " Files' name is invalid or does not exist (1205). " );
302 else
303 return file;
304 }
305
306 public int getCount()
307 {
308 return m_counter;
309 }
310
311 public long getSize()
312 throws IOException
313 {
314 long l = 0L ;
315 for ( int i = 0 ; i < m_counter; i ++ )
316 l += getFile(i).getSize();
317
318 return l;
319 }
320
321 public Collection getCollection()
322 {
323 return m_files.values();
324 }
325
326 public Enumeration getEnumeration()
327 {
328 return m_files.elements();
329 }
330 }
331
332
333 Request.java
334
335 import java.util.Enumeration;
336 import java.util.Hashtable;
337
338 public class Request
339 {
340
341 private Hashtable m_parameters;
342 private int m_counter;
343
344 Request()
345 {
346 m_parameters = new Hashtable();
347 m_counter = 0 ;
348 }
349
350 protected void putParameter(String s, String s1)
351 {
352 if (s == null )
353 throw new IllegalArgumentException( " The name of an element cannot be null. " );
354 if (m_parameters.containsKey(s))
355 {
356 Hashtable hashtable = (Hashtable)m_parameters.get(s);
357 hashtable.put( new Integer(hashtable.size()), s1);
358 } else
359 {
360 Hashtable hashtable1 = new Hashtable();
361 hashtable1.put( new Integer( 0 ), s1);
362 m_parameters.put(s, hashtable1);
363 m_counter ++ ;
364 }
365 }
366
367 public String getParameter(String s)
368 {
369 if (s == null )
370 throw new IllegalArgumentException( " Form's name is invalid or does not exist (1305). " );
371 Hashtable hashtable = (Hashtable)m_parameters.get(s);
372 if (hashtable == null )
373 return null ;
374 else
375 return (String)hashtable.get( new Integer( 0 ));
376 }
377
378 public Enumeration getParameterNames()
379 {
380 return m_parameters.keys();
381 }
382
383 public String[] getParameterValues(String s)
384 {
385 if (s == null )
386 throw new IllegalArgumentException( " Form's name is invalid or does not exist (1305). " );
387 Hashtable hashtable = (Hashtable)m_parameters.get(s);
388 if (hashtable == null )
389 return null ;
390 String as[] = new String[hashtable.size()];
391 for ( int i = 0 ; i < hashtable.size(); i ++ )
392 as[i] = (String)hashtable.get( new Integer(i));
393
394 return as;
395 }
396 }
397
398
399 SmartUploadException .java
400
401 public class SmartUploadException extends Exception
402 {
403
404 SmartUploadException(String s)
405 {
406 super (s);
407 }
408 }
409
410
411 SmartUpload.java
412
413 import java.io. * ;
414 import java.sql.ResultSet;
415 import java.sql.SQLException;
416 import java.util.Vector;
417 import javax.servlet. * ;
418 import javax.servlet.http. * ;
419 import javax.servlet.jsp.JspWriter;
420 import javax.servlet.jsp.PageContext;
421
422 // Referenced classes of package com.jspsmart.upload:
423 // Files, Request, SmartUploadException, File
424
425 public class SmartUpload
426 {
427
428 protected byte m_binArray[];
429 protected HttpServletRequest m_request;
430 protected HttpServletResponse m_response;
431 protected ServletContext m_application;
432 private int m_totalBytes;
433 private int m_currentIndex;
434 private int m_startData;
435 private int m_endData;
436 private String m_boundary;
437 private long m_totalMaxFileSize;
438 private long m_maxFileSize;
439 private Vector m_deniedFilesList;
440 private Vector m_allowedFilesList;
441 private boolean m_denyPhysicalPath;
442 private boolean m_forcePhysicalPath;
443 private String m_contentDisposition;
444 public static final int SAVE_AUTO = 0 ;
445 public static final int SAVE_VIRTUAL = 1 ;
446 public static final int SAVE_PHYSICAL = 2 ;
447 private Files m_files;
448 private Request m_formRequest;
449
450 public SmartUpload()
451 {
452 m_totalBytes = 0 ;
453 m_currentIndex = 0 ;
454 m_startData = 0 ;
455 m_endData = 0 ;
456 m_boundary = new String();
457 m_totalMaxFileSize = 0L ;
458 m_maxFileSize = 0L ;
459 m_deniedFilesList = new Vector();
460 m_allowedFilesList = new Vector();
461 m_denyPhysicalPath = false ;
462 m_forcePhysicalPath = false ;
463 m_contentDisposition = new String();
464 m_files = new Files();
465 m_formRequest = new Request();
466 }
467
468 public final void init(ServletConfig servletconfig)
469 throws ServletException
470 {
471 m_application = servletconfig.getServletContext();
472 }
473
474 public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
475 throws ServletException, IOException
476 {
477 m_request = httpservletrequest;
478 m_response = httpservletresponse;
479 }
480
481 public final void initialize(ServletConfig servletconfig, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
482 throws ServletException
483 {
484 m_application = servletconfig.getServletContext();
485 m_request = httpservletrequest;
486 m_response = httpservletresponse;
487 }
488
489 public final void initialize(PageContext pagecontext)
490 throws ServletException
491 {
492 m_application = pagecontext.getServletContext();
493 m_request = (HttpServletRequest)pagecontext.getRequest();
494 m_response = (HttpServletResponse)pagecontext.getResponse();
495 }
496
497 public final void initialize(ServletContext servletcontext, HttpSession httpsession, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, JspWriter jspwriter)
498 throws ServletException
499 {
500 m_application = servletcontext;
501 m_request = httpservletrequest;
502 m_response = httpservletresponse;
503 }
504
505 public void upload()
506 throws ServletException, IOException, SmartUploadException
507 {
508 int i = 0 ;
509 boolean flag = false ;
510 long l = 0L ;
511 boolean flag1 = false ;
512 String s = new String();
513 String s2 = new String();
514 String s4 = new String();
515 String s5 = new String();
516 String s6 = new String();
517 String s7 = new String();
518 String s8 = new String();
519 String s9 = new String();
520 String s10 = new String();
521 boolean flag2 = false ;
522 m_totalBytes = m_request.getContentLength();
523 m_binArray = new byte [m_totalBytes];
524 int j;
525 for (; i < m_totalBytes; i += j)
526 try
527 {
528 m_request.getInputStream();
529 j = m_request.getInputStream().read(m_binArray, i, m_totalBytes - i);
530 }
531 catch (Exception exception)
532 {
533 throw new SmartUploadException( " Unable to upload. " );
534 }
535
536 for (; ! flag1 && m_currentIndex < m_totalBytes; m_currentIndex ++ )
537 if (m_binArray[m_currentIndex] == 13 )
538 flag1 = true ;
539 else
540 m_boundary = m_boundary + ( char )m_binArray[m_currentIndex];
541
542 if (m_currentIndex == 1 )
543 return ;
544 for (m_currentIndex ++ ; m_currentIndex < m_totalBytes; m_currentIndex = m_currentIndex + 2 )
545 {
546 String s1 = getDataHeader();
547 m_currentIndex = m_currentIndex + 2 ;
548 boolean flag3 = s1.indexOf( " filename " ) > 0 ;
549 String s3 = getDataFieldValue(s1, " name " );
550 if (flag3)
551 {
552 s6 = getDataFieldValue(s1, " filename " );
553 s4 = getFileName(s6);
554 s5 = getFileExt(s4);
555 s7 = getContentType(s1);
556 s8 = getContentDisp(s1);
557 s9 = getTypeMIME(s7);
558 s10 = getSubTypeMIME(s7);
559 }
560 getDataSection();
561 if (flag3 && s4.length() > 0 )
562 {
563 if (m_deniedFilesList.contains(s5))
564 throw new SecurityException( " The extension of the file is denied to be uploaded (1015). " );
565 if ( ! m_allowedFilesList.isEmpty() && ! m_allowedFilesList.contains(s5))
566 throw new SecurityException( " The extension of the file is not allowed to be uploaded (1010). " );
567 if (m_maxFileSize > 0L && ( long )((m_endData - m_startData) + 1 ) > m_maxFileSize)
568 throw new SecurityException( " Size exceeded for this file : " + s4 + " (1105). " );
569 l += (m_endData - m_startData) + 1 ;
570 if (m_totalMaxFileSize > 0L && l > m_totalMaxFileSize)
571 throw new SecurityException( " Total File Size exceeded (1110). " );
572 }
573 if (flag3)
574 {
575 com.facet.jspsmart.upload.File file = new com.facet.jspsmart.upload.File();
576 file.setParent( this );
577 file.setFieldName(s3);
578 file.setFileName(s4);
579 file.setFileExt(s5);
580 file.setFilePathName(s6);
581 file.setIsMissing(s6.length() == 0 );
582 file.setContentType(s7);
583 file.setContentDisp(s8);
584 file.setTypeMIME(s9);
585 file.setSubTypeMIME(s10);
586 if (s7.indexOf( " application/x-macbinary " ) > 0 )
587 m_startData = m_startData + 128 ;
588 file.setSize((m_endData - m_startData) + 1 );
589 file.setStartData(m_startData);
590 file.setEndData(m_endData);
591 m_files.addFile(file);
592 } else
593 {
594 String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1 );
595 m_formRequest.putParameter(s3, s11);
596 }
597 if (( char )m_binArray[m_currentIndex + 1 ] == ' - ' )
598 break ;
599 }
600
601 }
602
603 public int save(String s)
604 throws ServletException, IOException, SmartUploadException
605 {
606 return save(s, 0 );
607 }
608
609 public int save(String s, int i)
610 throws ServletException, IOException, SmartUploadException
611 {
612 int j = 0 ;
613 if (s == null )
614 s = m_application.getRealPath( " / " );
615 if (s.indexOf( " / " ) != - 1 )
616 {
617 if (s.charAt(s.length() - 1 ) != ' / ' )
618 s = s + " / " ;
619 } else
620 if (s.charAt(s.length() - 1 ) != ' \\ ' )
621 s = s + " \\ " ;
622 for ( int k = 0 ; k < m_files.getCount(); k ++ )
623 if ( ! m_files.getFile(k).isMissing())
624 {
625 m_files.getFile(k).saveAs(s + m_files.getFile(k).getFileName(), i);
626 j ++ ;
627 }
628
629 return j;
630 }
631
632 public int getSize()
633 {
634 return m_totalBytes;
635 }
636
637 public byte getBinaryData( int i)
638 {
639 byte byte0;
640 try
641 {
642 byte0 = m_binArray[i];
643 }
644 catch (Exception exception)
645 {
646 throw new ArrayIndexOutOfBoundsException( " Index out of range (1005). " );
647 }
648 return byte0;
649 }
650
651 public Files getFiles()
652 {
653 return m_files;
654 }
655
656 public Request getRequest()
657 {
658 return m_formRequest;
659 }
660
661 public void downloadFile(String s)
662 throws ServletException, IOException, SmartUploadException
663 {
664 downloadFile(s, null , null );
665 }
666
667 public void downloadFile(String s, String s1)
668 throws ServletException, IOException, SmartUploadException, SmartUploadException
669 {
670 downloadFile(s, s1, null );
671 }
672
673 public void downloadFile(String s, String s1, String s2)
674 throws ServletException, IOException, SmartUploadException
675 {
676 downloadFile(s, s1, s2, 65000 );
677 }
678
679 public void downloadFile(String s, String s1, String s2, int i)
680 throws ServletException, IOException, SmartUploadException
681 {
682 if (s == null )
683 throw new IllegalArgumentException( " File ' " + s + " ' not found (1040). " );
684 if (s.equals( "" ))
685 throw new IllegalArgumentException( " File ' " + s + " ' not found (1040). " );
686 if ( ! isVirtual(s) && m_denyPhysicalPath)
687 throw new SecurityException( " Physical path is denied (1035). " );
688 if (isVirtual(s))
689 s = m_application.getRealPath(s);
690 java.io.File file = new java.io.File(s);
691 FileInputStream fileinputstream = new FileInputStream(file);
692 long l = file.length();
693 boolean flag = false ;
694 int k = 0 ;
695 byte abyte0[] = new byte [i];
696 if (s1 == null )
697 m_response.setContentType( " application/x-msdownload " );
698 else
699 if (s1.length() == 0 )
700 m_response.setContentType( " application/x-msdownload " );
701 else
702 m_response.setContentType(s1);
703 m_response.setContentLength(( int )l);
704 m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : " attachment; " ;
705 if (s2 == null )
706 m_response.setHeader( " Content-Disposition " , m_contentDisposition + " filename= " + getFileName(s));
707 else
708 if (s2.length() == 0 )
709 m_response.setHeader( " Content-Disposition " , m_contentDisposition);
710 else
711 m_response.setHeader( " Content-Disposition " , m_contentDisposition + " filename= " + s2);
712 while (( long )k < l)
713 {
714 int j = fileinputstream.read(abyte0, 0 , i);
715 k += j;
716 m_response.getOutputStream().write(abyte0, 0 , j);
717 }
718 fileinputstream.close();
719 }
720
721 public void downloadField(ResultSet resultset, String s, String s1, String s2)
722 throws ServletException, IOException, SQLException
723 {
724 if (resultset == null )
725 throw new IllegalArgumentException( " The RecordSet cannot be null (1045). " );
726 if (s == null )
727 throw new IllegalArgumentException( " The columnName cannot be null (1050). " );
728 if (s.length() == 0 )
729 throw new IllegalArgumentException( " The columnName cannot be empty (1055). " );
730 byte abyte0[] = resultset.getBytes(s);
731 if (s1 == null )
732 m_response.setContentType( " application/x-msdownload " );
733 else
734 if (s1.length() == 0 )
735 m_response.setContentType( " application/x-msdownload " );
736 else
737 m_response.setContentType(s1);
738 m_response.setContentLength(abyte0.length);
739 if (s2 == null )
740 m_response.setHeader( " Content-Disposition " , " attachment; " );
741 else
742 if (s2.length() == 0 )
743 m_response.setHeader( " Content-Disposition " , " attachment; " );
744 else
745 m_response.setHeader( " Content-Disposition " , " attachment; filename= " + s2);
746 m_response.getOutputStream().write(abyte0, 0 , abyte0.length);
747 }
748
749 public void fieldToFile(ResultSet resultset, String s, String s1)
750 throws ServletException, IOException, SmartUploadException, SQLException
751 {
752 try
753 {
754 if (m_application.getRealPath(s1) != null )
755 s1 = m_application.getRealPath(s1);
756 InputStream inputstream = resultset.getBinaryStream(s);
757 FileOutputStream fileoutputstream = new FileOutputStream(s1);
758 int i;
759 while ((i = inputstream.read()) != - 1 )
760 fileoutputstream.write(i);
761 fileoutputstream.close();
762 }
763 catch (Exception exception)
764 {
765 throw new SmartUploadException( " Unable to save file from the DataBase (1020). " );
766 }
767 }
768
769 private String getDataFieldValue(String s, String s1)
770 {
771 String s2 = new String();
772 String s3 = new String();
773 int i = 0 ;
774 boolean flag = false ;
775 boolean flag1 = false ;
776 boolean flag2 = false ;
777 s2 = s1 + " = " + ' " ' ;
778 i = s.indexOf(s2);
779 if (i > 0 )
780 {
781 int j = i + s2.length();
782 int k = j;
783 s2 = " \ "" ;
784 int l = s.indexOf(s2, j);
785 if (k > 0 && l > 0 )
786 s3 = s.substring(k, l);
787 }
788 return s3;
789 }
790
791 private String getFileExt(String s)
792 {
793 String s1 = new String();
794 int i = 0 ;
795 int j = 0 ;
796 if (s == null )
797 return null ;
798 i = s.lastIndexOf( 46 ) + 1 ;
799 j = s.length();
800 s1 = s.substring(i, j);
801 if (s.lastIndexOf( 46 ) > 0 )
802 return s1;
803 else
804 return "" ;
805 }
806
807 private String getContentType(String s)
808 {
809 String s1 = new String();
810 String s2 = new String();
811 int i = 0 ;
812 boolean flag = false ;
813 s1 = " Content-Type: " ;
814 i = s.indexOf(s1) + s1.length();
815 if (i != - 1 )
816 {
817 int j = s.length();
818 s2 = s.substring(i, j);
819 }
820 return s2;
821 }
822
823 private String getTypeMIME(String s)
824 {
825 String s1 = new String();
826 int i = 0 ;
827 i = s.indexOf( " / " );
828 if (i != - 1 )
829 return s.substring( 1 , i);
830 else
831 return s;
832 }
833
834 private String getSubTypeMIME(String s)
835 {
836 String s1 = new String();
837 int i = 0 ;
838 boolean flag = false ;
839 i = s.indexOf( " / " ) + 1 ;
840 if (i != - 1 )
841 {
842 int j = s.length();
843 return s.substring(i, j);
844 } else
845 {
846 return s;
847 }
848 }
849
850 private String getContentDisp(String s)
851 {
852 String s1 = new String();
853 int i = 0 ;
854 int j = 0 ;
855 i = s.indexOf( " : " ) + 1 ;
856 j = s.indexOf( " ; " );
857 s1 = s.substring(i, j);
858 return s1;
859 }
860
861 private void getDataSection()
862 {
863 boolean flag = false ;
864 String s = new String();
865 int i = m_currentIndex;
866 int j = 0 ;
867 int k = m_boundary.length();
868 m_startData = m_currentIndex;
869 m_endData = 0 ;
870 while (i < m_totalBytes)
871 if (m_binArray[i] == ( byte )m_boundary.charAt(j))
872 {
873 if (j == k - 1 )
874 {
875 m_endData = ((i - k) + 1 ) - 3 ;
876 break ;
877 }
878 i ++ ;
879 j ++ ;
880 } else
881 {
882 i ++ ;
883 j = 0 ;
884 }
885 m_currentIndex = m_endData + k + 3 ;
886 }
887
888 private String getDataHeader()
889 {
890 int i = m_currentIndex;
891 int j = 0 ;
892 boolean flag = false ;
893 for ( boolean flag1 = false ; ! flag1;)
894 if (m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2 ] == 13 )
895 {
896 flag1 = true ;
897 j = m_currentIndex - 1 ;
898 m_currentIndex = m_currentIndex + 2 ;
899 } else
900 {
901 m_currentIndex ++ ;
902 }
903
904 String s = new String(m_binArray, i, (j - i) + 1 );
905 return s;
906 }
907
908 private String getFileName(String s)
909 {
910 String s1 = new String();
911 String s2 = new String();
912 int i = 0 ;
913 boolean flag = false ;
914 boolean flag1 = false ;
915 boolean flag2 = false ;
916 i = s.lastIndexOf( 47 );
917 if (i != - 1 )
918 return s.substring(i + 1 , s.length());
919 i = s.lastIndexOf( 92 );
920 if (i != - 1 )
921 return s.substring(i + 1 , s.length());
922 else
923 return s;
924 }
925
926 public void setDeniedFilesList(String s)
927 throws ServletException, IOException, SQLException
928 {
929 String s1 = "" ;
930 if (s != null )
931 {
932 String s2 = "" ;
933 for ( int i = 0 ; i < s.length(); i ++ )
934 if (s.charAt(i) == ' , ' )
935 {
936 if ( ! m_deniedFilesList.contains(s2))
937 m_deniedFilesList.addElement(s2);
938 s2 = "" ;
939 } else
940 {
941 s2 = s2 + s.charAt(i);
942 }
943
944 if (s2 != "" )
945 m_deniedFilesList.addElement(s2);
946 } else
947 {
948 m_deniedFilesList = null ;
949 }
950 }
951
952 public void setAllowedFilesList(String s)
953 {
954 String s1 = "" ;
955 if (s != null )
956 {
957 String s2 = "" ;
958 for ( int i = 0 ; i < s.length(); i ++ )
959 if (s.charAt(i) == ' , ' )
960 {
961 if ( ! m_allowedFilesList.contains(s2))
962 m_allowedFilesList.addElement(s2);
963 s2 = "" ;
964 } else
965 {
966 s2 = s2 + s.charAt(i);
967 }
968
969 if (s2 != "" )
970 m_allowedFilesList.addElement(s2);
971 } else
972 {
973 m_allowedFilesList = null ;
974 }
975 }
976
977 public void setDenyPhysicalPath( boolean flag)
978 {
979 m_denyPhysicalPath = flag;
980 }
981
982 public void setForcePhysicalPath( boolean flag)
983 {
984 m_forcePhysicalPath = flag;
985 }
986
987 public void setContentDisposition(String s)
988 {
989 m_contentDisposition = s;
990 }
991
992 public void setTotalMaxFileSize( long l)
993 {
994 m_totalMaxFileSize = l;
995 }
996
997 public void setMaxFileSize( long l)
998 {
999 m_maxFileSize = l;
1000 }
1001
1002 protected String getPhysicalPath(String s, int i)
1003 throws IOException
1004 {
1005 String s1 = new String();
1006 String s2 = new String();
1007 String s3 = new String();
1008 boolean flag = false ;
1009 s3 = System.getProperty( " file.separator " );
1010 if (s == null )
1011 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
1012 if (s.equals( "" ))
1013 throw new IllegalArgumentException( " There is no specified destination file (1140). " );
1014 if (s.lastIndexOf( " \\ " ) >= 0 )
1015 {
1016 s1 = s.substring( 0 , s.lastIndexOf( " \\ " ));
1017 s2 = s.substring(s.lastIndexOf( " \\ " ) + 1 );
1018 }
1019 if (s.lastIndexOf( " / " ) >= 0 )
1020 {
1021 s1 = s.substring( 0 , s.lastIndexOf( " / " ));
1022 s2 = s.substring(s.lastIndexOf( " / " ) + 1 );
1023 }
1024 s1 = s1.length() != 0 ? s1 : " / " ;
1025 java.io.File file = new java.io.File(s1);
1026 if (file.exists())
1027 flag = true ;
1028 if (i == 0 )
1029 {
1030 if (isVirtual(s1))
1031 {
1032 s1 = m_application.getRealPath(s1);
1033 if (s1.endsWith(s3))
1034 s1 = s1 + s2;
1035 else
1036 s1 = s1 + s3 + s2;
1037 return s1;
1038 }
1039 if (flag)
1040 {
1041 if (m_denyPhysicalPath)
1042 throw new IllegalArgumentException( " Physical path is denied (1125). " );
1043 else
1044 return s;
1045 } else
1046 {
1047 throw new IllegalArgumentException( " This path does not exist (1135). " );
1048 }
1049 }
1050 if (i == 1 )
1051 {
1052 if (isVirtual(s1))
1053 {
1054 s1 = m_application.getRealPath(s1);
1055 if (s1.endsWith(s3))
1056 s1 = s1 + s2;
1057 else
1058 s1 = s1 + s3 + s2;
1059 return s1;
1060 }
1061 if (flag)
1062 throw new IllegalArgumentException( " The path is not a virtual path. " );
1063 else
1064 throw new IllegalArgumentException( " This path does not exist (1135). " );
1065 }
1066 if (i == 2 )
1067 {
1068 if (flag)
1069 if (m_denyPhysicalPath)
1070 throw new IllegalArgumentException( " Physical path is denied (1125). " );
1071 else
1072 return s;
1073 if (isVirtual(s1))
1074 throw new IllegalArgumentException( " The path is not a physical path. " );
1075 else
1076 throw new IllegalArgumentException( " This path does not exist (1135). " );
1077 } else
1078 {
1079 return null ;
1080 }
1081 }
1082
1083 public void uploadInFile(String s)
1084 throws IOException, SmartUploadException
1085 {
1086 int i = 0 ;
1087 int j = 0 ;
1088 boolean flag = false ;
1089 if (s == null )
1090 throw new IllegalArgumentException( " There is no specified destination file (1025). " );
1091 if (s.length() == 0 )
1092 throw new IllegalArgumentException( " There is no specified destination file (1025). " );
1093 if ( ! isVirtual(s) && m_denyPhysicalPath)
1094 throw new SecurityException( " Physical path is denied (1035). " );
1095 i = m_request.getContentLength();
1096 m_binArray = new byte [i];
1097 int k;
1098 for (; j < i; j += k)
1099 try
1100 {
1101 k = m_request.getInputStream().read(m_binArray, j, i - j);
1102 }
1103 catch (Exception exception)
1104 {
1105 throw new SmartUploadException( " Unable to upload. " );
1106 }
1107
1108 if (isVirtual(s))
1109 s = m_application.getRealPath(s);
1110 try
1111 {
1112 java.io.File file = new java.io.File(s);
1113 FileOutputStream fileoutputstream = new FileOutputStream(file);
1114 fileoutputstream.write(m_binArray);
1115 fileoutputstream.close();
1116 }
1117 catch (Exception exception1)
1118 {
1119 throw new SmartUploadException( " The Form cannot be saved in the specified file (1030). " );
1120 }
1121 }
1122
1123 private boolean isVirtual(String s)
1124 {
1125 if (m_application.getRealPath(s) != null )
1126 {
1127 java.io.File file = new java.io.File(m_application.getRealPath(s));
1128 return file.exists();
1129 } else
1130 {
1131 return false ;
1132 }
1133 }
1134 }