黑马程序员技术交流社区
标题:
TCP上传图片怎么变小了
[打印本页]
作者:
古银平
时间:
2012-10-31 23:07
标题:
TCP上传图片怎么变小了
本帖最后由 古银平 于 2012-11-5 11:57 编辑
import java.net.*;
import java.io.*;
class PicClient {
public static void main(String[] args)
{
Socket s = null;
try {
s = new Socket("192.168.1.101",10001);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = null;
BufferedOutputStream bosOut = null;
BufferedReader brIn = null;
try {
bosOut = new BufferedOutputStream(s.getOutputStream());
brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
bis = new BufferedInputStream(new FileInputStream("d:\\1.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int buf = 0;
while ((buf = bis.read())!=-1)
{
bosOut.write(buf);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String str = brIn.readLine();
System.out.println("Server"+str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
bis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<DIV class=blockcode>
<BLOCKQUOTE>import java.net.*;
import java.io.*;
class picServer {
public static void main(String[] args)
{
ServerSocket ss = null;
try {
ss = new ServerSocket(10001);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket s = null;
try {
s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"........connected");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bisIn = null;
BufferedOutputStream bos = null;
BufferedWriter bwOut = null;
try {
bisIn = new BufferedInputStream(s.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("d:\\2.jpg"));
bwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int buf = 0;
while((buf = bisIn.read())!=-1)
{
bos.write(buf);
}
bwOut.write("上传成功!");
bwOut.newLine();
bwOut.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
bwOut.close();
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
复制代码
}
结果为什么,图片变小了啊,
未命名.jpg
(9.63 KB, 下载次数: 11)
下载附件
2012-10-31 23:07 上传
作者:
卍解
时间:
2012-11-4 20:37
名字不一样也有能导致大小不一样,试试储存时名字一样,看大小是不是一样大。
作者:
王晓州
时间:
2012-11-5 10:23
字符流内部封装了数组,先读取两个字节查码表后刷新。
而字节流因为本身就是字节所以不用刷新,但你用的是字节流缓冲区,而在服务端读取客户端发送的数据时,客户端的流并没有关闭,所以要用到flush()方法。
故37行后面加一句bosOut.flush();就OK了。
至于服务端可加可不加,因为最后服务端关闭了流。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2