关于一个java一个服务器上传图片的问题
classFuwuduan{publicstaticvoidmain(String[]args)throwsException{ServerSocketss=newSer...
class Fuwuduan
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(7777);
while(true)
{
Socket s = ss.accept();
new Thread(new FuwuduanThread(s)).start();
}
}
}
class FuwuduanThread implements Runnable
{
private Socket s;
FuwuduanThread(Socket s)
{
this.s = s;
}
public void run()
{
int num = 1;
try
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"已连接...");
File file = new File(ip+"("+num+")"+".bmp");
while(file.exists())
file = new File(ip+"("+(num++)+")"+".bmp");
BufferedInputStream bufIn = new BufferedInputStream(s.getInputStream());
//InputStream in = s.getInputStream();
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(file));
//FileOutputStream out = new FileOutputStream("zs.jpg");
byte[] buy = new byte[1024*1024];
int len = 0;
while((len = bufIn.read(buy)) != -1)
{
bufOut.write(buy,0,len);
}
OutputStream os = s.getOutputStream();
os.write("上传成功".getBytes());
bufIn.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException("上传失败");
}
}
}
我这个服务器是一直开着的 然后出现个个问题 就是i我上传成功后的图片 不能打开 提示改图片正在另一个程序中运行,也就是服务器中运行着呢 如果我想打开图片必须要把服务器给关了 有什么办法可以不关服务器也能查看图片吗 展开
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(7777);
while(true)
{
Socket s = ss.accept();
new Thread(new FuwuduanThread(s)).start();
}
}
}
class FuwuduanThread implements Runnable
{
private Socket s;
FuwuduanThread(Socket s)
{
this.s = s;
}
public void run()
{
int num = 1;
try
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"已连接...");
File file = new File(ip+"("+num+")"+".bmp");
while(file.exists())
file = new File(ip+"("+(num++)+")"+".bmp");
BufferedInputStream bufIn = new BufferedInputStream(s.getInputStream());
//InputStream in = s.getInputStream();
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(file));
//FileOutputStream out = new FileOutputStream("zs.jpg");
byte[] buy = new byte[1024*1024];
int len = 0;
while((len = bufIn.read(buy)) != -1)
{
bufOut.write(buy,0,len);
}
OutputStream os = s.getOutputStream();
os.write("上传成功".getBytes());
bufIn.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException("上传失败");
}
}
}
我这个服务器是一直开着的 然后出现个个问题 就是i我上传成功后的图片 不能打开 提示改图片正在另一个程序中运行,也就是服务器中运行着呢 如果我想打开图片必须要把服务器给关了 有什么办法可以不关服务器也能查看图片吗 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询