今天面试一家公司,技术人员给我了一道socket编程题!我用了一下午的时间把这个做出来了,小有成就感!呵呵!说实话,socket以前只是看别人做过一些事例!自己到是没怎么用过!好了,废话少说,下面是我学习的过程!
首先,我先把需求说清楚,就是做一个网站上传文件,然后上传来的文件,通过socket把它放到直定的服务器上面; 网站要用struts ,我用的是struts2.0
第一步,我先把这个小网站给建好!因为以前做过网站上传这一块,一会就把这个工作给完成了!呵呵!
首先有一个欢迎页面: index.jsp页面
<%@ page language="java" pageEncoding="GBk"%> <%@ taglib prefix="s" uri="/struts-tags" %>
然后就是一个上传页面 upload.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage=""%> <%@ taglib prefix="s" uri="/struts-tags"%>
然后是一个上传失败页面 false.jsp
<%@ page language="java" pageEncoding="GBK"%>
上传失败~~~
请重新上传
在然后就是上传成功页面了 success.jsp
<%@ taglib prefix="s" uri="/struts-tags"%> 上传成功
" />
第二步页面已经做好了!然后就是代码了!上传的代码是
package com.hp.web.actions;
import java.io.File; import java.io.IOException;
import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial") public class Upload extends ActionSupport {
private File file; private String fileFileName; private String fileContentType;
public File getFile() { return file; }
public void setFile(File file) { this.file = file; }
public String getFileFileName() { return fileFileName; }
public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; }
public String getFileContentType() { return fileContentType; }
public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; }
@SuppressWarnings({ "static-access", "deprecation" }) public String upload() { String root = ServletActionContext.getRequest().getRealPath("/upload/"); try { File destFile = new File(root, this.getFileFileName()); FileUtils.copyFile(file, destFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return this.INPUT; } return this.SUCCESS; }
}
下面就是struts.xml配置
method="upload"> success.jsp false.jsp method="openServer"> index.jsp index.jsp method="saveServer"> upload.jsp
还有web.xml的配置
struts2 org.apache.struts2.dispatcher.FilterDispatcher struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp struts-cleanup /* struts2 /*
第三就是学习socket,听说google上面的技术文章挺多的,我就搜了搜结果还真搜到了,不过他做的是从服务器下载到客服端;这是真好的,要是自己写估计要到明天了,呵呵!不过他写的没说明,不过为了学习吧!我还是一个自己敲了一边!好了!这些费话就不说了!
首先,先了解一下socket吧!我就看了一篇文章, 通过这篇文章知道了socket是怎么工作的,然后把代码还是自己敲了一边!这个时候最是了解这个东西了!呵呵!看来我的学习能力还不错!学习这个过程就不说了!
下面是我写的socket 服务端和客户端!
服务端
package com.hp.web.socket;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.net.ServerSocket; import java.net.Socket;
public class ServerWeb { private ServerSocket serverSocket = null; private Socket socket = null;
public ServerWeb() { if(serverSocket ==null||serverSocket.isBound()){ this.openServer(); } }
public void openServer(){ try { serverSocket = new ServerSocket(10001);// 设置端口 while (true) { System.out.println("socket 开启~~~~"); socket = serverSocket.accept(); this.keep(); } } catch (Exception e) { e.printStackTrace(); } }
public void keep() { DataInputStream inputStream = null; String path = "C:/Users/hp/java/"; String fileName = ""; try { inputStream = new DataInputStream(new BufferedInputStream(socket .getInputStream())); fileName = inputStream.readUTF(); } catch (Exception e) { System.out.println("数据接收失败"); return; } try { byte[] buffer = new byte[10240]; int passedlength = 0; long length = 0; path = path + fileName; // System.out.println(path+" path " +inputStream.readUTF()); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream(new BufferedOutputStream( new FileOutputStream(path)))); length = inputStream.readLong();
while ((passedlength = inputStream.read(buffer)) > 0) { fileOut.write(buffer, 0, passedlength); } fileOut.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); }
} }
客户端
package com.hp.web.socket;
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.Socket;
public class ClientWeb {
private String path = null; Socket socket = null; private String ip = "localhost";// 设置成服务器IP private int port = 10001;
public ClientWeb(String _path) { try { if (createConnection()) { this.path = _path; send(); } } catch (Exception ex) { ex.printStackTrace(); } }
private boolean createConnection() {
try { socket = new Socket(ip, port); System.out.print("连接服务器成功!~~~~~~"); return true; } catch (Exception e) { System.out.print("连接服务器失败!!!!!!!!"); return false; }
}
private void send() { if (socket == null && path == null) return; try { File file = new File(path);
DataInputStream dataInputStream = new DataInputStream(socket .getInputStream()); //dataInputStream.readByte();
DataInputStream fis = new DataInputStream(new BufferedInputStream( new FileInputStream(path))); DataOutputStream ps = new DataOutputStream(socket.getOutputStream()); ps.writeUTF(file.getName());// 将文件名传过去 writeutf 使用 UTF-8 修改版编码将一个字符串写入基础输出流 ps.flush(); // 清空数据流 ps.writeLong((long) file.length()); ps.flush();
byte[] buffer = new byte[10240]; int length = 0; while ((length = fis.read(buffer)) > 0) { ps.write(buffer, 0, length); }
ps.flush();
// 关闭所有接连 ps.close(); fis.close(); dataInputStream.close(); socket.close(); } catch (Exception e) { System.out.println("信息传输过程中失败!"); } } }
其实这个两个类,最主要还是io流的方面的知识!以前对这方面还算是有点了解,所以理解起来不算困难!好了,但是最主要是还是中间的相互调用!哎!以后用的多了就知道什么意思了!好了就到这里吧!