Java tutorial
/** * Copyright (c) 2008-2014 FeiLong, Inc. All Rights Reserved. * <p> * This software is the confidential and proprietary information of FeiLong Network Technology, Inc. ("Confidential Information"). <br> * You shall not disclose such Confidential Information and shall use it * only in accordance with the terms of the license agreement you entered into with FeiLong. * </p> * <p> * FeiLong MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. <br> * FeiLong SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * </p> */ package com.feilong.controller; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * * * @author <a href="mailto:venusdrogon@163.com"></a> * @version 1.0 2012-3-18 ?11:32:05 */ @Controller @SuppressWarnings("all") public class DownloadController { @SuppressWarnings("unused") private static final Logger log = LoggerFactory.getLogger(DownloadController.class); @RequestMapping("/download") public String pager(Model model, HttpServletRequest request) { return "taglibTest/pagerTest"; } public HttpServletResponse download(String path, HttpServletResponse response) { try { // path File file = new File(path); // ??? String filename = file.getName(); // ???? String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // ?? InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // response response.reset(); // responseHeader response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; } public void downloadLocal(HttpServletResponse response) throws FileNotFoundException { // String fileName = "Operator.doc".toString(); // ??? // ? InputStream inStream = new FileInputStream("c:/Operator.doc");// // ? response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // ??? byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void downloadNet(HttpServletResponse response) throws MalformedURLException { // int bytesum = 0; int byteread = 0; URL url = new URL("windine.blogdriver.com/logo.gif"); try { URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); FileOutputStream fs = new FileOutputStream("c:/abc.gif"); byte[] buffer = new byte[1204]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // ??? public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception { File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte[] buf = new byte[1024]; int len = 0; response.reset(); // ??? if (isOnLine) { // ? URL u = new URL("file:///" + filePath); response.setContentType(u.openConnection().getContentType()); response.setHeader("Content-Disposition", "inline; filename=" + f.getName()); // ????UTF-8 } else { // ? response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + f.getName()); } OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) > 0) out.write(buf, 0, len); br.close(); out.close(); } // ?-? public static final HashMap<String, String> mFileTypes = new HashMap<String, String>(); static { // images mFileTypes.put("FFD8FF", "jpg"); mFileTypes.put("89504E47", "png"); mFileTypes.put("47494638", "gif"); mFileTypes.put("49492A00", "tif"); mFileTypes.put("424D", "bmp"); // mFileTypes.put("41433130", "dwg"); // CAD mFileTypes.put("38425053", "psd"); mFileTypes.put("7B5C727466", "rtf"); // mFileTypes.put("3C3F786D6C", "xml"); mFileTypes.put("68746D6C3E", "html"); mFileTypes.put("44656C69766572792D646174653A", "eml"); // mFileTypes.put("D0CF11E0", "doc"); mFileTypes.put("5374616E64617264204A", "mdb"); mFileTypes.put("252150532D41646F6265", "ps"); mFileTypes.put("255044462D312E", "pdf"); mFileTypes.put("504B0304", "docx"); mFileTypes.put("52617221", "rar"); mFileTypes.put("57415645", "wav"); mFileTypes.put("41564920", "avi"); mFileTypes.put("2E524D46", "rm"); mFileTypes.put("000001BA", "mpg"); mFileTypes.put("000001B3", "mpg"); mFileTypes.put("6D6F6F76", "mov"); mFileTypes.put("3026B2758E66CF11", "asf"); mFileTypes.put("4D546864", "mid"); mFileTypes.put("1F8B08", "gz"); mFileTypes.put("", ""); mFileTypes.put("", ""); } /** * ??? * * @param filePath * * @return ? */ public static String getFileType(String filePath) { return mFileTypes.get(getFileHeader(filePath)); } /** * ??? * * @param filePath * * @return ? */ public static String getFileHeader(String filePath) { FileInputStream is = null; String value = null; try { is = new FileInputStream(filePath); byte[] b = new byte[4]; is.read(b, 0, b.length); value = bytesToHexString(b); } catch (Exception e) { } finally { if (null != is) { try { is.close(); } catch (IOException e) { } } } return value; } /** * ???byte??string * * @param src * ???byte * @return ? */ private static String bytesToHexString(byte[] src) { StringBuilder builder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } String hv; for (int i = 0; i < src.length; i++) { // ?? 16????? hv = Integer.toHexString(src[i] & 0xFF).toUpperCase(); if (hv.length() < 2) { builder.append(0); } builder.append(hv); } return builder.toString(); } public static void main(String[] args) throws Exception { final String fileType = getFileType("E://Java?.docx"); System.out.println(fileType); } }