解决方案 »
- Java ClassLoader机制引发的WinForm程序加载类的一些疑惑? 求解答
- 没有输出 请大家帮忙看看 感谢
- First Aid!!!!关于过滤XML非法字符串的问题!!!0Xffff
- 问个import 问题 搞了一天了
- 菜鸟提问
- 经典的Java课程录制课件
- 关于Class.forName("mypackage.myclass").newInstance();的问题
- Swing中有没有类似richTextBox的东西?
- 把文件分割成2个。为什么后面的文件是0个字节
- UltraEdit 怎样 配置工具 appletviewer
- 新手问个jdk源码的问题
- 正则表达式如何表示 或者 和 空格这两个意思?
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient {
public Socket getS() {
return s;
}
public void setS(Socket s) {
this.s = s;
}
private Socket s;
private InputStream in;
private OutputStream out;
private BufferedInputStream inByte;
private OutputStream outByte;
private BufferedReader inStr;
private PrintWriter outStr;
public ObjectOutputStream oos = null;
public ObjectInputStream ois = null;
private long size = 0;
public SocketClient(String ip, int port) {
try {
s = new Socket(ip, port);
in = s.getInputStream();
out = s.getOutputStream();
inByte = new BufferedInputStream(in);
outByte = out;
oos = new ObjectOutputStream(out);
inStr = new BufferedReader(new InputStreamReader(in,"UTF-8"));
outStr = new PrintWriter(new OutputStreamWriter(out));
} catch (Exception e) {
e.printStackTrace();
}
}
public Object readObj(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(in);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Object obj = null;
try {
obj = ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
public void writeObj(Object obj){
try {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(obj);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readStr() throws IOException {
synchronized (this.in) {
return this.inStr.readLine();
}
}
public void writeStr(String content,String MyThreadname) {
synchronized (this.out) {
String IP = GetMyUserIP.getMyIP();
outStr.write(content+"丗"+IP+"丗"+MyThreadname);
System.out.println(content+"丗"+IP+"丗"+MyThreadname);
outStr.flush();
outStr.close();
}
}
public File readToFile(File file) throws IOException {
synchronized (this.in) {
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = this.inByte.read(temp))) {
fos.write(temp, 0, count);
fos.flush();
}
fos.close();
return file;
}
}
public void writeFile(File file) {
synchronized (this.out) {
size = file.length();
this.noticeFileSize(size);
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = fis.read(temp))) {
this.outByte.write(temp, 0, count);
this.outByte.flush();
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
// long progress = 0;
// while (size != (progress = getServerReceiveSize())) {
// //////"progress : " + (progress / (size / 100)) + "%");
// }
}
}
private void noticeFileSize(long l) {
String str = l + "";
int j = 19 - str.length();
for (int i = 0; i < j; i++) {
str = "0" + str;
}
this.writeByByte(str);
}
protected void writeByByte(String content) {
synchronized (this.out) {
try {
this.out.write(content.getBytes());
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private long getServerReceiveSize() {
synchronized (in) {
byte[] b = new byte[19];
try {
this.in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
return Long.parseLong(new String(b));
}
}
public String getProgress() {
long l = this.getServerReceiveSize() / (size / 100);
if(100 == l) {
return null;
}
return l + " %";
}
public void getMyResourceBack(){
if(inStr!=null){
try {
inStr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(outStr!=null){
outStr.flush();
outStr.close();
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class SocketServer {
private ServerSocket ss = null;
private Map<Socket, BufferedReader> rm = new HashMap<Socket, BufferedReader>();
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
BufferedReader br = null;
PrintWriter pw = null;
public SocketServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket nextSocket() {
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
public String read(Socket s) throws IOException {
if (null == (br = rm.get(s))) {
br = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
rm.put(s, br);
}
String s0 = br.readLine();
System.out.println("br.readLine()===="+s0);
return s0;
}
public Object readObject(Socket s){
Object obj = null;
try {
ois = new ObjectInputStream(s.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
obj = ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
public void writeObject(Socket s,Object obj){
try {
oos = new ObjectOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(obj);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(Socket s, String content) throws IOException {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println(content);
pw.flush();
}
public void getMyResourceBack(){
if(pw!=null){
pw.close();
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rm!=null){
rm.clear();
}if(ss!=null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;import com.sun.awt.AWTUtilities;public class MyImgComponent extends JLabel { private boolean isView = false;
private long lastTime = -1;
public static final int h;
public static final int w;
static {
Insets scrInsets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
h = scrSize.height - scrInsets.bottom - scrInsets.top;
w = scrSize.width - scrInsets.left - scrInsets.right;
} private String imgPath; public MyImgComponent(Icon icon) {
this.setIcon(icon);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(new Date().getTime() - lastTime > 800) {
lastTime = new Date().getTime();
return;
} else {
lastTime = -1;
}
if(isView) {
return;
}
isView = true;
final JWindow jf = new JWindow();
final JWindow jf2 = new JWindow();
JLabel jl = new JLabel();
try {
Icon i = new ImageIcon(new URL("file:///" + imgPath));
jl.setSize(i.getIconWidth(), i.getIconHeight());
jl.setIcon(i);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
JPanel jp2 = new JPanel();
jp2.setSize(jl.getWidth() + 300, jl.getHeight() + 300);
jp2.setBackground(Color.black);
jf.add(jl);
jf2.add(jp2);
jf.setSize(jl.getWidth(), jl.getHeight());
jf2.setSize(jp2.getWidth(), jp2.getHeight());
jf.setLocation((MyImgComponent.w - jl.getWidth()) / 2, (MyImgComponent.h - jl.getHeight()) / 2);
jf2.setLocation((MyImgComponent.w - jp2.getWidth()) / 2, (MyImgComponent.h - jp2.getHeight()) / 2);
AWTUtilities.setWindowOpacity(jf2, 0.8f);
JFrame.setDefaultLookAndFeelDecorated(true);
AWTUtilities.setWindowShape(jf2, new RoundRectangle2D.Double(0.0D, 0.0D, jf2.getWidth(), jf2.getHeight(), 20.0D, 20.0D));
jf.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jf.dispose();
jf2.dispose();
isView = false;
}
});
jf2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jf.dispose();
jf2.dispose();
isView = false;
}
});
jf2.setVisible(true);
jf.setVisible(true);
} });
} public void setIcon(Icon icon) {
if (null != icon) {
imgPath = icon.toString().replaceFirst("file:/", "");
}
super.setIcon(icon);
}}
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;import javax.imageio.ImageIO;/**
* 剪切板工具类
*
* @author NeverChange
*
*/
public class ClipboardUtil { private static List<MyAbstractClipboardProccess> ps = new ArrayList<MyAbstractClipboardProccess>();
public static final String TEMP_PATH; static {
TEMP_PATH = System.getProperty("java.io.tmpdir") + "MyClipboardTemp";
File f = new File(TEMP_PATH);
if (!f.exists()) {
f.mkdir();
} else if (!f.isDirectory()) {
f.delete();
f = new File(TEMP_PATH);
f.mkdir();
} ps.add(new MyAbstractClipboardProccess() { public boolean isSupport(Object content) {
return content.toString().trim().startsWith("<html")
|| content.toString().trim().startsWith("<HTML");
} public MyImgTextContent getContent(Object content) {
content = content.toString().trim().replaceAll("(\r|\n)+", "")
.replaceAll("<!--(.)*?-->", "").replaceAll("<!(.)*?>",
"").replaceAll("<style>.*?</style>", "")
.replaceAll("<STYLE>.*?</STYLE>", "").replaceAll(
"</p>", "\n").replaceAll("</P>", "\n")
.replaceAll("<br>", "\n").replaceAll("<br/>", "\n")
.replaceAll("<IMG", "<img").replaceAll(
"<(?!img).*?(/)?>", "")
.replaceAll("</.+?>", "");
MyImgTextContent mtc = new MyImgTextContent();
for (String str : content.toString().split("<")) {
if (str.startsWith("img")) {
mtc.put("img", saveImgToMyTemp(str.split("file:///")[1]
.split("\"")[0]));
if (!str.endsWith(">")) {
mtc.put("text", escapeHtml(str.split(">")[str
.split(">").length - 1]));
}
} else {
if (!"".equals(str)) {
mtc.put("text", escapeHtml(str.split("file:///")[0]
.split("\"")[0]));
}
}
}
return mtc;
} });// 默认剪切板处理类, 支持解析word&qq
ps.add(new MyAbstractClipboardProccess() { public boolean isSupport(Object content) {
return content instanceof java.awt.image.BufferedImage;
} public MyImgTextContent getContent(Object content) {
MyImgTextContent mtc = new MyImgTextContent();
mtc.put("img", saveImgToMyTemp((BufferedImage) content));
return mtc;
} });// 剪切板图片处理类, 支持解析word&qq截图
ps.add(new MyAbstractClipboardProccess() { public boolean isSupport(Object content) {
return content instanceof java.lang.String;
} public MyImgTextContent getContent(Object content) {
MyImgTextContent mtc = new MyImgTextContent();
mtc.put("text", content.toString());
return mtc;
} });// 剪切板文字处理类, 支持解析word&qq
// 使用ps.add();相应的实现类扩展其他软件的支持
} /**
* 封装剪切板图文混排内容
*
* @author NeverChange
*
*/
public static class MyImgTextContent implements Iterable<String>, Serializable{
List<String> content = new ArrayList<String>();
private String MytopSocketInfo = "";
private String MysubSocketInfo = "";
public List<String> getContent() {
return content;
} public void setContent(List<String> content) {
this.content = content;
} public String getMytopSocketInfo() {
return MytopSocketInfo;
} public void setMytopSocketInfo(String mytopSocketInfo) {
MytopSocketInfo = mytopSocketInfo;
} public String getMysubSocketInfo() {
return MysubSocketInfo;
} public void setMysubSocketInfo(String mysubSocketInfo) {
MysubSocketInfo = mysubSocketInfo;
} public String getMybottomSocketInfo() {
return MybottomSocketInfo;
} public void setMybottomSocketInfo(String mybottomSocketInfo) {
MybottomSocketInfo = mybottomSocketInfo;
} private String MybottomSocketInfo = ""; public void put(String type, String content) {
this.content.add(type + "-" + content);
} public int hashCode() {
return content.hashCode();
} public boolean equals(Object obj) {
return content.equals(obj);
} public String toString() {
StringBuffer str = new StringBuffer();
int ct = 0;
for (String c : content) {
str.append("\n" + (ct++) + " : " + c);
} return str.toString();
} public Iterator<String> iterator() {
return content.iterator();
}
} private static abstract class MyAbstractClipboardProccess {
public abstract boolean isSupport(Object content); public abstract MyImgTextContent getContent(Object content); public void fileCopy(File source, File target) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(target); int ct = 0;
byte[] bs = new byte[1024 * 8];
while (-1 != (ct = fis.read(bs))) {
fos.write(bs, 0, ct);
}
fis.close();
} catch (Exception e) {
if (null != fis) {
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
} public String escapeHtml(String html) {
return null == html ? null : html.replaceAll("\\"", "\"")
.replaceAll("\\&", "&").replaceAll("\\<", "<")
.replaceAll("\\>", ">").replaceAll("\\ ", " ");
} public String saveImgToMyTemp(String path) {
try {
File source = new File(path);
if (source.exists() && source.isFile()) {
File target = new File(TEMP_PATH + "\\"
+ UUID.randomUUID().toString() + "."
+ path.split("\\.")[path.split("\\.").length - 1]);
target.createNewFile();
this.fileCopy(source, target);
return target.getPath();
}
} catch (IOException e) {
e.printStackTrace();
} return null;
} public String saveImgToMyTemp(BufferedImage img) {
try {
File target = new File(TEMP_PATH + "\\"
+ UUID.randomUUID().toString() + ".jpg");
target.createNewFile();
FileOutputStream fos = new FileOutputStream(target);
ImageIO.write(img, "jpg", fos);
fos.flush();
fos.close();
return target.getPath();
} catch (Exception e) {
e.printStackTrace();
} return null;
}
} public static MyImgTextContent getMyImgTextContentFromClipboard() {
Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable cc = sysc.getContents(null);
for(DataFlavor df : sysc.getAvailableDataFlavors()) {
try {
for(MyAbstractClipboardProccess mcpi : ps) {
if(mcpi.isSupport(cc.getTransferData(df))) {
return mcpi.getContent(cc.getTransferData(df));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}}
请问,我在进行MyServer3类型的运行后,再次运行MyClient3类型,为什么会有下图的“前置乱码”出现:请求组织点拨我这个问题的产生原因.
代码亲测可以运行.
String str = ss.read(s);
char[] ch = str.toCharArray();
System.out.println("ch.length===="+ch.length);
String[] buffer = str.split("丗");
byte[] by = str.getBytes();
byte[] byt = new byte[by.length-8];
for(int i =0;i<by.length;i++){
byt[i]=by[i+8];
}
String MyObjIP = buffer[0];
String MyObjNum = buffer[1];
但是我在这里的地址:http://bbs.csdn.net/topics/390200842
中1#的高手的回答,在进行String转byte数组的操作时,“不采用Base64”进行转换,会有:丢失数据的问题出现.
但是我在转换的过程中,“没有数据丢失的印象”.
希望高手点拨:
如果我的本楼贴出的问题,“很麻烦”的话,那么,小弟的“本贴的”“治标”的方案.如果进行实施的话,是不是一定要用Base64这个工具类型来进行我的“治标方案”的实施?
如果组织能够帮助小弟,小弟,还是祈求帮助我的高手,能够把小弟的“本楼的问题产生原因”,“治本”的方案,告诉小弟.
Java,我的理想,希望CSDN的老师们,朋友们,能够让小弟圆梦:
成为祖国信息产业的一位,建设者.
System.out.println("br.readLine()===="+s0);这个打印什么?发送端是已UTF-8发的吗?
再浏览了下你使用的发送组件是:ObjectOutputStream,该组件会先再流中输出前导编码(用于标识限界),从你截图来看很像是这个问题。所以接收时也必须严格使用ObjectInputStream来进行接收。
以上为猜测,请结合你自己程序编写思路考虑是否为此问题。
System.out.println("br.readLine()===="+s0);这个打印什么?发送端是已UTF-8发的吗?
withiter版主,小弟的发送端的写出数据的方法如下.
public void writeStr(String content,String MyThreadname) {
synchronized (this.out) {
String IP = GetMyUserIP.getMyIP();
outStr.write(content+"丗"+IP+"丗"+MyThreadname);
System.out.println(content+"丗"+IP+"丗"+MyThreadname);
outStr.flush();
outStr.close();
}
}
您说的语句的打印结果如下:小弟希望组织,能够继续点拨小弟的“茁壮成长”.
再浏览了下你使用的发送组件是:ObjectOutputStream,该组件会先再流中输出前导编码(用于标识限界),从你截图来看很像是这个问题。所以接收时也必须严格使用ObjectInputStream来进行接收。
ldh911老师,谢谢您的亲自点拨!!!!
学生用的组件,发出方是:
PrintWriter.
接收方是:
BufferedReader
小弟希望ldh911大哥,能够点拨小弟的这个技术点.
让小弟,成佛.
学生用的组件,发出方是:
PrintWriter.
接收方是:
BufferedReader
小弟希望ldh911大哥,能够点拨小弟的这个技术点.
让小弟,成佛.
绝对好用。
希望ldh911大哥,能够为小弟的这个作品的这个环节的“核心挑战”,做一下技术指导!!
感谢withiter版主!!感谢CSDN!!感谢组织的培养!!
感谢12#楼的讲解技术平易近人的eccel大哥的二次讲解!!
也感谢多次培育学生的CSDN金牌版主,withiter!!!!
有刘德华911大哥,eccel大哥,withiter大哥!!无祖国软件工程Java屌丝梦想不落地开花结果!!!!!!
刘德华911大哥,eccel大哥与withiter大哥,,就是CSDN的梦想金牌教练!!!!
小弟因为CSDN有您们的存在,而感到:
无限荣耀!!!!!!
CSDN!!!!壹佰万岁!!!!!!