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
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