📄 photoserver.java
字号:
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.Socket;import java.net.ServerSocket;import java.sql.*;public class PhotoServer { private int port = 1929; private ServerSocket serverSocket; static final String dbURL = "jdbc:mysql://localhost/j2mebook?" + "user=j2meapps&password=bigsecret"; public PhotoServer() throws ClassNotFoundException { Class.forName("org.gjt.mm.mysql.Driver"); } public void acceptConnections() { try { serverSocket = new ServerSocket(port); } catch (IOException e) { System.err.println("ServerSocket instantiation failure"); e.printStackTrace(); System.exit(0); } while (true) { try { Socket newConnection = serverSocket.accept(); System.out.println("accepted connection"); ServerThread st = new ServerThread(newConnection); new Thread(st).start(); } catch (IOException ioe) { System.err.println("server accept failed"); } } } public static void main(String args[]) { PhotoServer server = null; try { server = new PhotoServer(); } catch (ClassNotFoundException e) { System.out.println("unable to load JDBC driver"); e.printStackTrace(); System.exit(1); } server.acceptConnections(); } class ServerThread implements Runnable { private Socket socket; private DataInputStream datain; private DataOutputStream dataout; public ServerThread(Socket socket) { this.socket = socket; } public void run() { try { datain = new DataInputStream(new BufferedInputStream (socket.getInputStream())); dataout = new DataOutputStream(new BufferedOutputStream (socket.getOutputStream())); } catch (IOException e) { return; } byte[] ba = new byte[50]; boolean conversationActive = true; while(conversationActive) { String name = null; try { datain.read(ba,0,50); name = new String(ba); if ((name.length() == 1) && (name.toUpperCase().charAt(0) == 'Q')) { conversationActive = false; } else { String fileName = getFileName(name.trim()); byte[] ia = getImage(fileName); String sl = Integer.toString(ia.length) + "\n"; System.out.println("sending length = " + sl); dataout.write(sl.getBytes(),0,sl.length()); dataout.flush(); System.out.println("sending " + ia.length + " bytes"); dataout.write(ia,0,ia.length); dataout.flush(); System.out.println("photo sent"); } } catch (IOException ioe) { conversationActive = false; } } try { System.out.println("closing socket"); datain.close(); dataout.close(); socket.close(); } catch (IOException e) { } } private String getFileName(String name) { String fileName = "nophoto"; Connection conn = null; try { conn = DriverManager.getConnection(dbURL); Statement stmt = conn.createStatement(); int ix = name.indexOf(","); String first = name.substring(ix + 1); String last = name.substring(0,ix); String query = "SELECT filename FROM photos " + "WHERE lastname = '" + last + "'" + " AND " + "firstname = '" + first + "'"; System.out.println("query = " + query); ResultSet rs = stmt.executeQuery(query); if (rs.next()) { fileName = rs.getString(1); } } catch (SQLException e) { System.out.println("ERROR: " + e.getMessage()); fileName = "error"; } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } return fileName + ".png"; } private byte[] getImage(String fname) { byte[] ba = new byte[0]; try { File f = new File("c:\\OMH\\photos\\" + fname); ba = new byte[(int)f.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); int nb = bis.read(ba,0,ba.length); System.out.println("nb = " + nb); bis.close(); } catch (IOException e) { System.out.println(e.getMessage()); } finally { return ba; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -