Java實(shí)現(xiàn)的簡單圖片上傳功能示例
本文實(shí)例講述了Java實(shí)現(xiàn)的簡單圖片上傳功能。分享給大家供大家參考,具體如下:
import java.io.*;
import java.net.*;
/*
*發(fā)送端
*/
class picsend
{
public static void main(String[] args) throws Exception
{
if(args.length!=1)
{
System.out.println("請選擇一張.jpg圖片");
return;
}
File file = new File(args[0]);
if (!(file.exists() && file.isFile()))
{
System.out.println("圖片有問題(不是文件或不存在)");
return;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("圖片格式不對,請重新選擇圖片");
return;
}
if(file.length()>1024*1024*10)
{
System.out.println("圖片過大,無法上傳");
return;
}
Socket s = new Socket("192.168.33.1",10006);//建立服務(wù)
FileInputStream fis = new FileInputStream("d:\\美女.jpg");//讀取圖片
OutputStream out = s.getOutputStream();//讀到的寫入
byte [] b = new byte[1024];
int len = 0;
while((len = fis.read(b))!= -1)
{
out.write(b,0,len);
}
s.shutdownOutput();//標(biāo)記結(jié)束
InputStream in = s.getInputStream();//讀服務(wù)端返回數(shù)據(jù)
byte [] bin = new byte[1024];
int num = in.read(bin);
System.out.println(new String(bin,0,num));
fis.close();
s.close();
}
}
class picThread implements Runnable
{
private Socket s;
picThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();//得到ip
try
{
System.out.println(ip+".............connect");
InputStream in = s.getInputStream();//讀到流中數(shù)據(jù)
File file = new File(ip+"("+(count)+")"+".jpg");
while(file.exists())//判斷文件是否存在
file = new File(ip+"("+(count++)+")"+".jpg");
FileOutputStream fos = new FileOutputStream(file);//寫入
byte [] b = new byte[1024];
int len = 0;
while((len = in.read(b))!=-1)
{
fos.write(b,0,len);
}
OutputStream out = s.getOutputStream();//寫入服務(wù)端傳過來數(shù)據(jù)
out.write("上傳成功!".getBytes());
fos.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException("上傳失敗");
}
}
}
/*
*服務(wù)端
*/
class picrece
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10006);
while(true)
{
Socket s = ss.accept();//接收
new Thread(new picThread(s)).start();
}
}
}
運(yùn)行效果圖如下:


更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對大家java程序設(shè)計有所幫助。
- Java實(shí)現(xiàn)圖片上傳至服務(wù)器功能(FTP協(xié)議)
- Java實(shí)現(xiàn)的圖片上傳工具類完整實(shí)例
- Java Struts圖片上傳至指定文件夾并顯示圖片功能
- java使用CKEditor實(shí)現(xiàn)圖片上傳功能
- Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例
- Java實(shí)現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來
- Java+mysql本地圖片上傳數(shù)據(jù)庫及下載示例
- java web圖片上傳和文件上傳實(shí)例
- java web圖片上傳和文件上傳實(shí)例詳解
- java實(shí)現(xiàn)圖片上傳至本地實(shí)例詳解
相關(guān)文章
解決@ServerEndpoint不能注入@Autowired的問題
這篇文章主要介紹了解決@ServerEndpoint不能注入@Autowired的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot+thymeleaf整合阿里云OOS對象存儲圖片的實(shí)現(xiàn)
本文主要介紹了springboot+thymeleaf整合阿里云OOS對象存儲圖片的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案
這篇文章主要介紹了詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
Spring Boot 整合 Shiro+Thymeleaf過程解析
這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10

