[英]sending an image from client to server in socket
i have this code that sends an text file from client and put it in server side how can I modify this code make it send an image file of type "png" instead of sending an text file .. how can i use bytes for this job我有这个代码,它从客户端发送一个文本文件并将它放在服务器端我如何修改这个代码使它发送一个“png”类型的图像文件而不是发送一个文本文件..我如何使用字节来完成这项工作
in the code it's copy the file content and put it into a string and then he wrote it again into a new string in the sever side the client file have more than one file and you will chose the text that you want to copy .在代码中,它复制文件内容并将其放入一个字符串中,然后他再次将其写入服务器端的一个新字符串中,客户端文件有多个文件,您将选择要复制的文本。
this is the server code这是服务器代码
public static void main(String[] args){
try{
ServerSocket serverSocket = new ServerSocket(2012);
System.out.print("Running");
Socket socket= serverSocket.accept();
System.out.println("Client connected");
PrintWriter pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String dir = "C:\\Users\\alaqra senpai\\Desktop\\FTP";
File f=new File(dir);
File fl[]=f.listFiles();
Arrays.sort(fl);
int c=0;
for(int i=0;i<fl.length;i++){
if(fl[i].canRead() && (fl[i].toString().endsWith(".txt"))){
c++;
}
}
pw.println(" "+c+".txt file founde , listed A to z");
for (int i=0;i<fl.length;i++){
if((fl[i].toString().endsWith(".txt"))){
pw.println(" "+(i) + " "+ fl[i].getName() + " " +fl[i].length()+"byte");
}
}
pw.println("_");
pw.flush();
String tem=br.readLine();
int temp=Integer.parseInt(tem);
temp-=48;
System.out.println("index " +temp);
boolean files=false;
int index=0;
if(temp>=0 && temp<=fl.length){
files=true;
index=temp;
}else{
files=false;
}
if(files){
try{
File ff=new File(fl[index].getAbsolutePath());
FileReader fr=new FileReader(ff);
BufferedReader brf=new BufferedReader(fr);
String s;
while((s=brf.readLine())!=null){
pw.println(s);
}
pw.flush();
if(brf.readLine()!=null){
System.out.println("File read succeful,closing socket");
}
}catch(IOException e ){
System.err.println("error"+e);
}
}
br.close();
socket.close();
}catch(IOException e){
}
}
and this is the client code这是客户端代码
public static void main(String[] args){
try {
//declaration and initialization client socket
Socket socket = new Socket(InetAddress.getByName("Localhost"), 2012);
//read and write on socket
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//read from console
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((br.read()) != '_') {
System.out.println(br.readLine());
}
System.out.println("enter file index no: ");
out.println(bu.read());
//force write buffer
out.flush();
//file receive process
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Recevie.png")));
while ((s = br.readLine()) != null) {
bw.write(s);
}
//force write buffer to server
bw.close();
if (br.readLine() == null) {
System.out.println("File Write Successful,Socket closing");
}
} catch (Exception e) {
System.out.println("Error" + e);
}
} catch (IOException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
}
}
Use FileInputStream and BufferedInputStream to as image is binary file.使用 FileInputStream 和 BufferedInputStream 作为图像是二进制文件。 Check more on using streams here - http://tutorials.jenkov.com/java-io/index.html , nicely explained java io.在此处查看有关使用流的更多信息 - http://tutorials.jenkov.com/java-io/index.html ,很好地解释了 java io。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.