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

📄 server.java

📁 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网!
💻 JAVA
字号:
	/* CRMS, customer relationship management system	Copyright (C) 2003  Service To Youth Council	This program is free software; you can redistribute it and/or modify	it under the terms of the GNU General Public License as published by	the Free Software Foundation; either version 2 of the License, or	(at your option) any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA	For further information contact the SYC ICT department on GPL@syc.net.au	98 Kermode Street	North Adelaide	South Australia	SA 5006 	+61 (0)8 8367 0755	*//* * Server.java * * Created on 1 April 2003, 00:25 */package crms.util;import java.net.*;import java.io.*;import java.beans.*;import crms.module.*;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;import org.apache.commons.httpclient.methods.multipart.*;import javax.swing.JOptionPane;import crms.applet.PanelManager;/** * * @author  dmurphy */public class Server {		private String hostName = "localhost";	private int port = 8081;	private String path = "crms/crmservlet";	private String uploadPath = "crms/fileupload";		private String user = null;		/** Creates a new instance of Server */	public Server(String hostName, int port, String path) {		this.hostName = hostName;		this.port = port;		this.path = path;	}		public void setUser(String user) {		this.user = user;	}		public String getUser() {		return user;	}		public ServerResponse sendCommand(ServerCommand command) {						try {			if (getUser() == null) {				throw new RuntimeException("Can't send command, no user set!");			}			command.setUser(getUser());			InetAddress addr = InetAddress.getByName(hostName);			java.net.Socket serverSocket = new java.net.Socket(addr, port);			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream(), "UTF8"));			StringBuffer encodedCommand = new StringBuffer();			ByteArrayOutputStream bOut = new ByteArrayOutputStream();			XMLEncoder encoder = new XMLEncoder(bOut);			encoder.writeObject(command);			encoder.flush();			encoder.close();			encodedCommand.append(URLEncoder.encode("command","UTF-8"));			encodedCommand.append("=");			encodedCommand.append(URLEncoder.encode(bOut.toString(),"UTF-8"));		   			StringBuffer buf = new StringBuffer();			buf.append("POST http://" + hostName + ":" + port + "/" + path +" HTTP/1.0\r\n");			buf.append("Content-Length: " + encodedCommand.length() + "\r\n");			buf.append("Content-Type: application/x-www-form-urlencoded\r\n");			buf.append("\r\n");			buf.append(encodedCommand);			out.write(buf.toString());			System.out.println("Sending data:");			System.out.println(buf.toString());			out.flush();			// Get response			BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));			StringBuffer response = new StringBuffer();			String line = in.readLine();			// First read until the blank line that indicates the end of the			// HTTP response headers			while (!line.equals("")) {				line = in.readLine();			}			// Now we've ignored the headers, read the response XML			line = in.readLine();			while (line != null) {				response.append(line);				line = in.readLine();			}			out.close();			in.close();			System.out.println("Got response: ");			System.out.println(response.toString());			XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(response.toString().getBytes()));			ServerResponse serverResponse = (ServerResponse) decoder.readObject();						if (serverResponse != null && serverResponse.getPart("denied") != null) {				JOptionPane.showMessageDialog(					PanelManager.getInstance().getParentPanel(), 					serverResponse.getPart("denied"),					"Security Error",					JOptionPane.ERROR_MESSAGE					);								PanelManager.getInstance().activatePanel(new crms.applet.HomePanel());				throw new SecurityException((String)serverResponse.getPart("denied"));				}						return serverResponse;		}		catch (Exception ex) {			ex.printStackTrace();			throw new RuntimeException(ex);		}	}		public ServerResponse sendCommand(FileSendCommand command) throws java.security.AccessControlException {						try {			if (getUser() == null) {				throw new RuntimeException("Can't send command, no user set!");			}			command.setUser(getUser());			HttpClient client = new HttpClient();						HostConfiguration config = new HostConfiguration();			config.setHost(new org.apache.commons.httpclient.URI("http://" + hostName + ":" + port));						client.setHostConfiguration(config);						MultipartPostMethod method = new MultipartPostMethod("http://" + hostName + ":" + port + "/" + uploadPath);						method.addPart(new StringPart(FileAttachmentModule.PARAM_REF_ID, command.getFileToSend().getReferenceID(),"UTF-8"));			method.addPart(new StringPart(FileAttachmentModule.PARAM_TYPE, command.getFileToSend().getAttachmentType().getCode(),"UTF-8"));			method.addPart(new StringPart(FileAttachmentModule.PARAM_DESCRIPTION, command.getFileToSend().getDescription(),"UTF-8"));			method.addPart(new StringPart(FileAttachmentModule.PARAM_ATTACHED_BY, command.getFileToSend().getAttachedBy(),"UTF-8"));			File file = command.getPhysicalLocation();						method.addPart(new FilePart(file.getName(), file));						int result = client.executeMethod(config, method);			System.out.println("Response is " + result);						return new ServerResponse("result", new Integer(result));		} catch (Exception ex) {			ex.printStackTrace();			return new ServerResponse("result", new Integer(-1));		}	}				public StringBuffer sendCommand(StringBuffer command) {		try {						StringBuffer encodedCommand = new StringBuffer();			encodedCommand.append(URLEncoder.encode("command","UTF-8"));			encodedCommand.append("=");			encodedCommand.append(URLEncoder.encode(command.toString(),"UTF-8"));		   						InetAddress addr = InetAddress.getByName(hostName);			java.net.Socket serverSocket = new java.net.Socket(addr,port);						BufferedWriter out = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream(), "UTF8"));						StringBuffer buf = new StringBuffer();						buf.append("POST http://" + hostName + ":" + port + "/" + path +" HTTP/1.0\r\n");			buf.append("Content-Length: " + encodedCommand.length() + "\r\n");			buf.append("Content-Type: application/x-www-form-urlencoded\r\n");			buf.append("\r\n");			/*			System.out.println( "-----------------");			System.out.println("Sending data: ");			System.out.println(buf.toString());			System.out.println(encodedCommand);			System.out.println( "-----------------");*/						out.write(buf.toString());						// Send data			out.write(encodedCommand.toString());			out.flush();						// Get response			BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));						StringBuffer response = new StringBuffer();						String line = in.readLine();						// First read until the blank line that indicates the end of the			// HTTP response headers						while (!line.equals("")) {				line = in.readLine();			}						// Now we've ignored the headers, read the response XML						line = in.readLine();			while (line != null) {				response.append(line);				line = in.readLine();			}						out.close();			in.close();						return response;					}		catch (Exception ex) {			throw new RuntimeException(ex);		}	}	}

⌨️ 快捷键说明

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