down.jsp
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:2k
源码类别:
Jsp/Servlet
开发平台:
Java
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ page import="java.io.*"%>
- <%!/**
- * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
- * @param s 原文件名
- * @return 重新编码后的文件名
- */
- 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");
- }
- %>
- <%
- %>