⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadservlet.java

📁 基于J2EE的办公自动化系统。实现流程定义流程办理等。运用了hibernate+struts+spring框架综合运用的系统。
💻 JAVA
字号:
package com.oa.module.folder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件下载
 * filename:文件名
 * filepath:文件路径
 * @author MY-PC
 *
 */
public class DownLoadServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		InputStream input = null;
		OutputStream output = null;
		try {
			String filename = request.getParameter("filename");
			 String filepath = request.getParameter("filepath");
			filename = toUtf8String(filename);
			 filepath = toUtf8String(filepath);

			System.out.println("2:" + filename);

			response.setContentType("application/x-msdownload;charset=gbk");
			response.setHeader("Content-disposition", "attachment; "
					+ "filename=" + filename);

			filename = Utf8URLdecode(filename);
			 filepath = Utf8URLdecode(filepath);

			System.out.println("3:" + filename);
			String ss = this.getServletContext().getRealPath(filepath+"/" + filename);
			System.out.println(ss);
			input = new FileInputStream(ss);
			output = response.getOutputStream();
			byte[] buff = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = input.read(buff, 0, buff.length)) != -1) {
				System.out.println((char) bytesRead);
				output.write(buff, 0, bytesRead);

			}
		} catch (Exception e) {
			 e.printStackTrace();
		}  finally {
		 if (input != null)
		 input.close();
		 if (output != null)
		 output.close();
		 }

	}

	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}

	/**
	 * Utf8URL解码
	 * 
	 * @param text
	 * @return
	 */
	public String Utf8URLdecode(String text) {
		String result = "";
		int p = 0;
		if (text != null && text.length() > 0) {
			text = text.toLowerCase();
			p = text.indexOf("%e");
			if (p == -1) {
				return text;
			}
			while (p != -1) {
				result += text.substring(0, p);
				text = text.substring(p, text.length());
				if (text == "" || text.length() < 9) {
					return result;
				}
				result += CodeToWord(text.substring(0, 9));// 调用CodeToWord方法
				text = text.substring(9, text.length());
				p = text.indexOf("%e");
			}
		}
		return result + text;
	}

	/**
	 * utf8URL编码转字符
	 * 
	 * @param text
	 * @return
	 */
	private String CodeToWord(String text) {
		String result;
		if (Utf8codeCheck(text)) {// 调用Utf8codeCheck方法
			byte[] code = new byte[3];
			code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
			code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
			code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
			try {
				result = new String(code, "UTF-8");
			} catch (Exception ex) {
				result = null;
			}
		} else {
			result = text;
		}
		return result;
	}

	/**
	 * 编码是否有效
	 * 
	 * @param text
	 * @return
	 */
	private boolean Utf8codeCheck(String text) {
		String sign = "";
		if (text.startsWith("%e")) {
			for (int i = 0, p = 0; p != -1; i++) {
				p = text.indexOf("%", p);
				if (p != -1) {
					p++;
				}
				sign += p;
			}
		}
		return sign.equals("147-1");
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -