这是我客户端的部分代码;是把图片存在手机SD卡上 然后在读取出来, 通过数据流传给服务器,然后再传给其他客户端, 数据流用的是
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);现在的问题是我不通过服务器,图片能显示出来。发送给服务器,在接受回来,就乱码了。 //发送图片
btn_image.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
//开启Pictures画面Type设定为image
intent.setType("image/*");
// 使用Intent.ACTION_GET_CONTENT这个Action
intent.setAction(Intent.ACTION_GET_CONTENT);
// 取得相片后返回本画面
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
System.out.println("uri"+uri.toString());
ContentResolver cr =getContentResolver();
try {
mcontent = readStream(cr.openInputStream(Uri.parse(uri.toString())));
String s1=new String(mcontent);
out.println("pic"+s1);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void ShowDialog(String msg) {
new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
public void run() {
try {
while (true) {
if(socket.isConnected()){
if(!socket.isInputShutdown()){
if ((content = in.readLine()) != null) {
mHandler.sendMessage(mHandler.obtainMessage());
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(content.contains("pic"))
{ String u=null;
u= content.substring(3);
myBitmap = getPicFromBytes(u.getBytes(), null);
HashMap<String,Object> map = null;
map = new HashMap<String, Object>();
map.put("icon",myBitmap);
map.put("text","");
mListData.add(map);
}else
{
HashMap<String,Object> map = null;
map = new HashMap<String, Object>();
map.put("icon",null);
map.put("text",content);
mListData.add(map);
}
}
};
public static Bitmap getPicFromBytes ( byte[] bytes , BitmapFactory.Options opts )
{
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream ( InputStream inStream ) throws Exception
{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}服务器的代码如下:
public class Main {
private static final int PORT = 9999;// 端口监听
private List<Socket> mList = new ArrayList<Socket>();// 存放客户端socket
private ServerSocket server = null;
private ExecutorService mExecutorService = null;// 线程池
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
public Main(){//构造函数!
try {
server = new ServerSocket(PORT);
mExecutorService = Executors.newCachedThreadPool();// 创建一个线程池
System.out.println("Server Start...");
Socket client = null;
while (true) {
client = server.accept(); //连接
mList.add(client); //讲客户端加入买list中 保存下来
mExecutorService.execute(new Service(client));// 开启一个客户端线程.
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public class Service implements Runnable{
private Socket socket;
private BufferedReader in = null;
private String msg = "";
public Service(Socket socket) { //构造函数!
this.socket = socket;
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
this.sendmsg();
} catch(IOException e){
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
while(true){
if((msg=in.readLine())!=null){
if(msg.equals("exit")){
System.out.println("ssssss");
mList.remove(socket);
in.close();
socket.close();
this.sendmsg();
break;
}else{
this.sendmsg();
}
}
}
}catch(Exception ex){
System.out.println("server 读取数据异常");
ex.printStackTrace();
}
}public void sendmsg(){
System.out.println(msg);
int num = mList.size();
for (int i = 0; i < num; i++) {
Socket mSocket = mList.get(i);
PrintWriter pout = null;
try{
pout = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(mSocket.getOutputStream())),
true);
pout.println(msg);
}catch(IOException e){
e.printStackTrace();
}
}
}
} }
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);现在的问题是我不通过服务器,图片能显示出来。发送给服务器,在接受回来,就乱码了。 //发送图片
btn_image.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
//开启Pictures画面Type设定为image
intent.setType("image/*");
// 使用Intent.ACTION_GET_CONTENT这个Action
intent.setAction(Intent.ACTION_GET_CONTENT);
// 取得相片后返回本画面
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
System.out.println("uri"+uri.toString());
ContentResolver cr =getContentResolver();
try {
mcontent = readStream(cr.openInputStream(Uri.parse(uri.toString())));
String s1=new String(mcontent);
out.println("pic"+s1);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void ShowDialog(String msg) {
new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
public void run() {
try {
while (true) {
if(socket.isConnected()){
if(!socket.isInputShutdown()){
if ((content = in.readLine()) != null) {
mHandler.sendMessage(mHandler.obtainMessage());
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(content.contains("pic"))
{ String u=null;
u= content.substring(3);
myBitmap = getPicFromBytes(u.getBytes(), null);
HashMap<String,Object> map = null;
map = new HashMap<String, Object>();
map.put("icon",myBitmap);
map.put("text","");
mListData.add(map);
}else
{
HashMap<String,Object> map = null;
map = new HashMap<String, Object>();
map.put("icon",null);
map.put("text",content);
mListData.add(map);
}
}
};
public static Bitmap getPicFromBytes ( byte[] bytes , BitmapFactory.Options opts )
{
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream ( InputStream inStream ) throws Exception
{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}服务器的代码如下:
public class Main {
private static final int PORT = 9999;// 端口监听
private List<Socket> mList = new ArrayList<Socket>();// 存放客户端socket
private ServerSocket server = null;
private ExecutorService mExecutorService = null;// 线程池
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
public Main(){//构造函数!
try {
server = new ServerSocket(PORT);
mExecutorService = Executors.newCachedThreadPool();// 创建一个线程池
System.out.println("Server Start...");
Socket client = null;
while (true) {
client = server.accept(); //连接
mList.add(client); //讲客户端加入买list中 保存下来
mExecutorService.execute(new Service(client));// 开启一个客户端线程.
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public class Service implements Runnable{
private Socket socket;
private BufferedReader in = null;
private String msg = "";
public Service(Socket socket) { //构造函数!
this.socket = socket;
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
this.sendmsg();
} catch(IOException e){
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
while(true){
if((msg=in.readLine())!=null){
if(msg.equals("exit")){
System.out.println("ssssss");
mList.remove(socket);
in.close();
socket.close();
this.sendmsg();
break;
}else{
this.sendmsg();
}
}
}
}catch(Exception ex){
System.out.println("server 读取数据异常");
ex.printStackTrace();
}
}public void sendmsg(){
System.out.println(msg);
int num = mList.size();
for (int i = 0; i < num; i++) {
Socket mSocket = mList.get(i);
PrintWriter pout = null;
try{
pout = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(mSocket.getOutputStream())),
true);
pout.println(msg);
}catch(IOException e){
e.printStackTrace();
}
}
}
} }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货