Forums Register Login

unable to retrieve ms word files from server socket

+Pie Number of slices to send: Send
good afternoon

iam getting the problem in retrieving word documents from server socket. even iam unable to upload pdf files to server.
but at present i want the solution for getting word documents..
my server program is

import java.io.*;
import java.net.*;
import java.util.*;
public class server
{
public static void main(String args[])
{
String fn=null;
String c=null;
String tmp=null;
try
{
ServerSocket ss=new ServerSocket(8081);
int count=1;
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
StringTokenizer st=new StringTokenizer(br.readLine());
while(st.hasMoreTokens())
{
tmp=st.nextToken();
fn=st.nextToken();
c=st.nextToken();
}
if(tmp.equals("copy"))
new PutRegistration(s,count++,fn,c).start();
else
new GetFile(s,count++,fn,c).start();
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class PutRegistration extends Thread
{
private Socket soc;
private int num;
private BufferedInputStream in;
private FileOutputStream fout=null;
int i;
File mndir,cdir,mdir,f;
boolean s,s1,s2;
String fn=null;
String c=null;
public PutRegistration(Socket soc,int num,String fn,String c)
{
this.soc=soc;
this.num=num;
this.fn=fn;
this.c=c;
System.out.println("thread"+num+"running");
try
{
in=new BufferedInputStream(soc.getInputStream());
mndir=new File("./courses");
if(!(mndir.exists()))
s=mndir.mkdir();
cdir=new File(mndir+"/"+c);
if(!cdir.exists())
s1=cdir.mkdir();
mdir=new File(cdir+"/material");
if(!mdir.exists())
s2=mdir.mkdir();

f=new File(mdir+"/"+fn);
fout=new FileOutputStream(f);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void run()
{
try
{
do{
i=in.read();
if(i!=-1)
{
fout.write((char)i);
}
}while(i!=-1);
fout.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}

class GetFile extends Thread
{
private Socket soc;
String fn;
String c;
int num;
File f;
int ch;
FileReader fr;
public GetFile(Socket soc,int num,String fn,String c)
{
this.soc=soc;
this.num=num;
this.fn=fn;
this.c=c;
System.out.println("thread"+num+"running");
try
{
f=new File("./courses/"+c+"/material/"+fn);
System.out.println(f.getPath());
PrintWriter toclient=new PrintWriter(soc.getOutputStream(),true);
if(f.exists())
{
fr=new FileReader(f);
while((ch=fr.read())!=-1)
{
toclient.print((char)ch);

toclient.flush();
}
}
fr.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

my client program for uploading to server is

<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.sql.*"%>
<%
String url="jdbc:odbc:demo";
String username="system";
String password="tiger";
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String classpath="sun.jdbc.odbc.JdbcOdbcDriver";
String query="select course_id from courses where ins_id=?";
String s=request.getParameter("file");
String id=(String)session.getAttribute("iid");
int i=s.lastIndexOf("\\");
String f=s.substring(i+1);
int j=f.lastIndexOf(".");
String type=f.substring(j+1);
String cid=null;
try
{
Class.forName(classpath);
con=DriverManager.getConnection(url,username,password);
ps=con.prepareStatement(query);
ps.setString(1,id);
rs=ps.executeQuery();
while(rs.next())
cid=rs.getString("course_id");
FileReader is=new FileReader(s);
Socket soc=new Socket("192.168.4.202",8081);
if((type!=null)&&(type.equals("msword")))
{
response.setContentType("application/msword");
}

PrintWriter toServer=new PrintWriter(soc.getOutputStream(),true);
int ch;
toServer.println("copy"+" "+f+" "+cid);
while ((ch = is.read()) >= 0)
{
toServer.print((char)ch);
toServer.flush();
}
}
catch(Exception e)
{
out.println(e.toString());
}
%>


my client program for downloading from server

<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@page contentType="application/msword" %>
<%
String fn=request.getParameter("shiva");
String cid=request.getParameter("c");
int i;
BufferedInputStream fromserver=null;
try
{
Socket soc=new Socket("192.168.4.202",8081);
PrintWriter toserver=new PrintWriter(soc.getOutputStream(),true);
toserver.println("read"+" "+fn+" "+cid);
fromserver=new BufferedInputStream(soc.getInputStream());
while((i=fromserver.read())!=-1)
{
out.print((char)i);
out.flush();
}
System.out.println("completly received ");
}
catch(Exception e)
{
out.println(e.toString());
}
%>


can anyone help me out please..
many thanks
+Pie Number of slices to send: Send
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will do so.

Looking at it briefly, it seems that you are using Reader and Writer classes - those won't work with binary files like DOC and PDF. You need to use Stream classes.

You're also using println with sockets, which is a bug waiting to happen: Don't println to a Socket
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1172 times.
Similar Threads
open a .exe in java (DLL Problem)
Why I can't write in a loop
Problem in wrtitng JAR File
How to Change the File Name for Each Uploaded Files to the Socket Server?
Help need for client server program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:32:39.