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

📄 handle.java

📁 用jsp实现的大型商城源代码
💻 JAVA
字号:
package myshop;

import javax.servlet.http.*;
import java.net.*;
import java.io.*;

public class Handle {
	//--- 处理null值参数 ---
	public String getString(HttpServletRequest request,String ParamName) {
		String t = request.getParameter(ParamName);
		if (t != null && !t.equals("")) return(t);
		else return("");
	}

	public String getString(HttpServletRequest request,String ParamName,String DefaultVal) {
		String t = request.getParameter(ParamName);
		if (t != null && !t.equals("")) return(t);
		else return DefaultVal;
	}

	public int getInt(HttpServletRequest request,String ParamName) {
		if (request.getParameter(ParamName) != null) return (Integer.parseInt(request.getParameter(ParamName)));
		else return(0);
	}

	//--- 内码转换,解决中文问题 ---
	public String GBK2ISO(String InputStr) throws Exception {
		return(new String(InputStr.getBytes("GBK"),"ISO8859_1"));
	}

	public String ISO2GBK(String InputStr) throws Exception {
		return(new String(InputStr.getBytes("ISO8859_1"),"GBK"));
	}

	//--- 替换函数 ---
	public String Replace(String OldStr,String NewStr,String SourceStr) {
		int i;
		StringBuffer buffer = new StringBuffer();

		i = SourceStr.indexOf(OldStr);
		if (i == -1) return(SourceStr);
		buffer.append(SourceStr.substring(0,i) + NewStr);
		if ((i + OldStr.length()) < SourceStr.length()) buffer.append(Replace(OldStr,NewStr,SourceStr.substring((i + OldStr.length()),SourceStr.length())));
		return(buffer.toString());
	}

	public String[] Split(String tag,String fieldsru) {
		char dot = tag.charAt(0);
		String field;
		field = fieldsru + dot;
		int num = 0;
		int field_len = field.length();
		for (int i = 0; i < field_len; i++) if (field.charAt(i) == dot) num++;
		String returnarray[] = new String[num];
		int begin = 0;
		int end;
		for (int j = 0; j < num; j++) {
			end = field.indexOf(dot, begin);
			returnarray[j] = field.substring(begin, end);
			begin = end + 1;
		}
		return(returnarray);
	}

	//--- 转换文本格式 ---
	public String ConvertChar(String InputStr) {
		return (Replace(" ","&nbsp;",Replace("\n","<br>",InputStr)));
	}

	//--- 还原文本格式 ---
	public String ReturnTxt(String InputStr) {
		return (Replace("&nbsp;"," ",Replace("<br>","\n",InputStr)));
	}

	//--- 创建一个cookie ---
	public void setCookie(HttpServletResponse response,String CookieName,String CookieVal,int CookieAge) {
		Cookie cookie = new Cookie(CookieName,URLEncoder.encode(CookieVal));
		cookie.setMaxAge(CookieAge);
		response.addCookie(cookie);
	}

	//--- 获得指定Cookie的值 ---
	public String getCookie(HttpServletRequest request,String CookieName) {
		Cookie cookies[] = request.getCookies();
		
		if (cookies == null) return(null);
		for (int i=0; i<cookies.length; i++) if (cookies[i].getName().equals(CookieName)) return(URLDecoder.decode(cookies[i].getValue()));
		return(null);
	}

	//--- 列举所有COOKIE ---
	public String getCookie(HttpServletRequest request) {
		String CookieStr="";
		Cookie cookies[] = request.getCookies();
		
		if (cookies == null) return(null);
		for (int i=0; i<cookies.length; i++) {
			CookieStr += cookies[i].getName() + " = " + URLDecoder.decode(cookies[i].getValue()) + "<br>";
		}
		return(CookieStr);
	}

	//--- 删除文件或文件夹 ---
	public int Del(HttpServletRequest request,String Name) {
		int flag=0;
		
		File f = new File(request.getRealPath(".") + "\\" + Name);
		if (!f.exists()) flag = 1;
		else if (!f.delete()) flag = 2;

		return(flag);
	}

	//--- 创建文件夹 ---
	public void MkDir(HttpServletRequest request,String FolderName) {
		File folder = new File(request.getRealPath(".") + "\\" + FolderName);
		
		if (!folder.exists()) folder.mkdirs();
	}

	//--- 创建一个文件 ---
	public void WriteFile(HttpServletRequest request,String FileName,String stream) throws Exception {
		FileWriter fileWriter = new FileWriter(request.getRealPath(".") + "\\" + FileName);
		PrintWriter printWriter = new PrintWriter(fileWriter);

		printWriter.println(stream);
		printWriter.flush();
		printWriter.close();
		fileWriter.close();	
	}

	//--- 读取一个文件 ---
	public String ReadFile(HttpServletRequest request,String FileName) throws Exception {
		String str="";
		int c;
		FileInputStream file = new FileInputStream(request.getRealPath(".") + "\\" + FileName);
		
		while ((c = file.read()) != -1) str += (char)c;
		file.close();
		str = ISO2GBK(str);
		return(str);
	}

	//--- 打开一个URL ---
	public String ReadUrl(String Inputurl) throws Exception {
		String sCurrentLine=""; 
		String sTotalString=""; 
		InputStream urlInputStream;
		URL url;
		HttpURLConnection urlConnection;
		BufferedReader BufferReader;
		//定点、打开、连接
		url = new URL(Inputurl);
		urlConnection = (HttpURLConnection)url.openConnection();
		urlConnection.connect(); 
		urlInputStream = urlConnection.getInputStream();
		//缓冲后可以以characters, arrays, and lines 方式读取数据。
		BufferReader = new BufferedReader(new InputStreamReader(urlInputStream)); 
		while ((sCurrentLine = BufferReader.readLine()) != null) sTotalString += sCurrentLine + "\n";
		BufferReader.close();

		return(sTotalString);
	}

	//--- 把日期重组成新闻页面路径 ---
	public String Convert2Path(int id,String date_time) {
		String path_file[] = new String[2];
		String path[] = new String[3];
		String file[] = new String[3];
		String pathStr,fileStr;

		path_file = Split(" ",date_time);
		path = Split("-",path_file[0]);
		file = Split(":",path_file[1]);
		
		pathStr = path[0] + path[1] + path[2];
		fileStr = file[0] + file[1] + file[2];
		return(pathStr + "/" + java.lang.String.valueOf(id) + fileStr + ".html");
	}
	
}

⌨️ 快捷键说明

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