VIEW.jsp 4.49 KB
Newer Older
Thitichaipun Wutthisak committed
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
<%@page import="java.io.*" %>
<%@page import="javax.activation.MimetypesFileTypeMap"%>
<%@page import="com.csc.library.utilities.*"%>
<%@page import="com.csc.library.util.download.*"%>
<%@page import="com.csc.library.session.InitialEnvironment" %>

<!-- http://127.0.0.1:8082/hr/VIEW.jsp?type=PERSONAL_UPLOAD&dir=dir&subfolder=PICTURE&filename=035058.gif -->
<%!

	/**
	 * Returns the Mime Type of the file, depending on the extension of the filename
	 */
	static String getMimeType(String fName) {
		String mimeType;
		fName = fName.toLowerCase();
		
		if (fName.endsWith(".jpg") || fName.endsWith(".jpeg") || fName.endsWith(".jpe")) {
			mimeType = "image/jpeg";
		} 
		else if (fName.endsWith(".gif")) { 
			mimeType = "image/gif";
		} 
		else if (fName.endsWith(".png")) { 
			mimeType = "image/png";
		} 
		else if (fName.endsWith(".bmp")) { 
			mimeType = "image/bmp";
		} 

		else if (fName.endsWith(".swf")) { 
			mimeType = "application/x-shockwave-flash";
		} 


		else if (fName.endsWith(".pdf")) { 
			mimeType = "application/pdf";
		} 
		else if (fName.endsWith(".zip")) { 
			mimeType = "application/zip";
		} 
		else if (fName.endsWith(".rtf")) { 
			mimeType = "application/rtf";
		} 
		else if (fName.endsWith(".xl") || fName.endsWith(".xls") || fName.endsWith(".xlv") 
				|| fName.endsWith(".xla") || fName.endsWith(".xlb") || fName.endsWith(".xlt") 
				|| fName.endsWith(".xlm") || fName.endsWith(".xlk")) { 
			mimeType = "application/vnd.ms-excel";
		} 
		else if (fName.endsWith(".doc") || fName.endsWith(".dot")) { 
			mimeType = "application/vnd.ms-word";
		} 
		else if (fName.endsWith(".ppt")) { 
			mimeType = "application/vnd.ms-powerpoint";
		} 
		
		else if (fName.endsWith(".htm") || fName.endsWith(".html") || fName.endsWith(".shtml")) { 
			mimeType = "text/html";
		} 
		
		else if (fName.endsWith(".xml")) { 
			mimeType = "text/xml";
		} 
		
		else if (fName.endsWith(".mid") || fName.endsWith(".midi")) { 
			mimeType = "audio/x-midi";
		} 
		else if (fName.endsWith(".mp3")) { 
			mimeType = "audio/mp3";
		} 
		else if (fName.endsWith(".ogg")) { 
			mimeType = "audio/ogg";
		}
		
		else if (fName.endsWith(".avi")) { 
			mimeType = "video/x-msvideo";
		} 
		else if (fName.endsWith(".mov") || fName.endsWith(".qt")) { 
			mimeType = "video/quicktime";
		}
		else if (fName.endsWith(".mpg") || fName.endsWith(".mpeg") || fName.endsWith(".mpe")) { 
			mimeType = "video/mpeg";
		} 

		else {
			mimeType = "text/plain";
		}
		
		return mimeType;
		
	}

%>
<%
	CheckNull chkNull = new CheckNull();
	String type = chkNull.chkNullString( request.getParameter("type") );
	String dir = chkNull.chkNullString( request.getParameter("dir")) ;
	String filename = chkNull.chkNullString( request.getParameter("filename") );
	
	if( type!=null && dir!=null && filename!=null ){
		InitialEnvironment env = new InitialEnvironment("GLOBAL");
		String upload_dir = env.getValue( type +"-" +dir );
		String file_dir = upload_dir +chkNull.chkNullString( request.getParameter("subfolder") );
		String default_filename = chkNull.chkNullString( request.getParameter("default") );
		
		if(default_filename.equals("")) {
			default_filename = chkNull.chkNullString( env.getValue( type +"-default" ) );
		}

		
		try{
			File file = new File( file_dir, filename );
			// check file
			if ( !file.isFile() || !file.exists() || !file.canRead()) {
				if( !default_filename.equals("")) {
					System.out.println("path=" +file.getCanonicalFile());
					System.out.println( "file=" +file.isFile() +", exists=" +file.exists() +", canRead=" +file.canRead());
					System.out.println("Error : Using default file: " +default_filename );

					file = new File( file_dir, default_filename );
				}
			}
			String mimeType = getMimeType( file.getName() );

			System.out.println("path=" +file.getCanonicalFile());
			System.out.println("mimeType=" +mimeType +", file=" +file.isFile() +", exists=" +file.exists() +", canRead=" +file.canRead());
			
			response.setContentType(mimeType);
			response.setHeader("Content-Disposition", "inline;filename=\"" + file.getName() + "\"");
			response.setContentLength((int) file.length());
			
			 BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(file));
             byte buffer[] = new byte[8 * 1024];
             out.clearBuffer();
             OutputStream out_s = response.getOutputStream();
	         int b;
			 while ((b = fileInput.read(buffer)) != -1)
			 	 out_s.write(buffer, 0, b);

			 out_s.flush();
			 fileInput.close();
             
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

%>