开启本地服务器,扫描二维码进行通信
前言:之前写过一个通过socket通信的软件,实现pc与android互相传送文件,android控制pc等等功能。现在想做一个使用http通信的(纯粹无聊),优点是不需要下载android客户端,并且希望能在ios端也可以使用(当然有待改进)。手机通过扫描产生的二维码来访问pc的客户端,再进行通信。
现在只处于demo阶段,后面有空陆续上传完整版。现在还是学生,有待指导。
1.使用的技术
Java开启Web服务器,使用zwing生成二维码
2.UI讲解
ui如下:
第一个框是ip
第二个框是工程web目录下的文件名:这里要把需要手机访问的内容添加在工程web目录下(未来可能改进自动获取)
点击make生成二维码,再用手机扫描
3. 代码讲解
start.java :启动main
SimpleHttpServer.java :开启http服务器
UI.java :UI界面
MatrixToImageWriter.java :生成二维码
SimpleHttpServer:
public class SimpleHttpServer implements Runnable {
//...
public static int PORT = 9528;// 端口 这里可以设置端口
//...
void fileReaderAndReturn(String fileName, Socket socket) throws IOException {
//...
//下面设置对应文件类型的处理:例如html:text/html 还有很多 具体可以百度contentType
if ("htmlhtmxml".indexOf(fileEx) > -1) { //(显示)匹配文件类型 , 如果是html,htm,xml..
contentType = "text/html;charset=UTF-8";
} else if ("jpeggifbpmpng".indexOf(fileEx) > -1) { // (下载)如果是jpeg,jpg,gif,bpm,png,pdf等等..
contentType = "application/binary";
}
// else if ("jpg".indexOf(fileEx) > -1) { // (图片显示)
// contentType = "image/JPG";
//}
else if("pdf".indexOf(fileEx) > -1){
contentType = "application/pdf";
}
else if("xml".indexOf(fileEx) > -1){
contentType = "text/xml";
}
}
//...
/** */
/**
* 启动 HTTP 服务器
*
* @param args
*/
public static void startServer() {
new SimpleHttpServer();
}
}
MatrixToImageWriter:
public final class MatrixToImageWriter {
// 导出文件
public static void writeToFile(BitMatrix matrix, String format, File file) {..}
//导出流
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) {..}
//最后我自己修改加了一个writeToBufferedImage
public static BufferedImage writeToBufferedImage(BitMatrix matrix, String format) {..}
}
4.Demo下载
5.参考
http://blog.youkuaiyun.com/klyun/article/details/1682943
http://www.2cto.com/kf/201302/187345.html