Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2000, 05:51 PM
narensv narensv is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 0 narensv User rank is Private First Class (20 - 50 Reputation Level)narensv User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0

Hi:
can anyone help me with the transfer of afile from one machine to another using servlets. I want a java program which when run from the dos prompt would transfer a file on my machine (location is hardcoded) to another machine and save it in a particular folder on that machine. both the machines are in the same network.

naren
Comments on this post
JimmyGosling agrees!
Gran Roguismo agrees!

Reply With Quote
  #2  
Old November 15th, 2000, 03:29 PM
ajayw ajayw is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 0 ajayw User rank is Private First Class (20 - 50 Reputation Level)ajayw User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Naren,
If u are looking to upload a file to another machine/server there are a couple of ways to go about it.
1) Jason hunter has a mutipart request/response @ www.servlets.com( for Form based file uploads)
2) You can use this code which is given in the o'reilly book

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadServlet extends HttpServlet {
static final int MAX_SIZE=10240000;
String rootPath, successMessage;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
rootPath=config.getInitParameter("RootPath");
if (rootPath==null)
{
rootPath="/";
}
successMessage=config.getInitParameter("SuccessMessage");
if (successMessage==null)
{
successMessage="File Upload Complete";
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
ServletOutputStream out=null;
DataInputStream in=null;
FileOutputStream fileOut=null;
try
{
response.setContentType("text/plain");
out=response.getOutputStream();
}
catch(IOException ie)
{
System.out.println("Error getting output stream");
System.out.println("Error description" +ie);
return;
}
try
{
String contentType=request.getContentType();
if (contentType!=null && contentType.indexOf("multipart/form-data")!=-1)
{
in = new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
if (formDataLength>MAX_SIZE)
{
out.println("Sorry the file is too large to upload");
out.flush();
return;
}
byte dataBytes[]=new byte[formDataLength];
int bytesRead=0;
int totalBytesRead=0;
while (totalBytesRead<formDataLength)
{
bytesRead=in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=bytesRead;
}
String file=new String(dataBytes);
dataBytes=null;
int lastIndex=contentType.lastIndexOf("=");
String boundary=contentType.substring(lastIndex+1,contentType.length());
String directory="";
if (file.indexOf("name="Directory"")>0)
{
directory=file.substring(file.indexOf(file.indexOf("name="Directory"")));
directory=directory.substring(directory.indexOf("n")+1);
directory=directory.substring(directory.indexOf("n")+1);
directory=directory.substring(0,directory.indexOf("n")-1);
if (directory.indexOf("..")>0)
{
out.println("Security Error Cannot upload to a directory higher in the tree");
return;
}
}
String successPage="";
if (file.indexOf("name="SuccessPage"")>0)
{
successPage=file.substring(file.indexOf("name="SuccessPage""));
successPage=successPage.substring(successPage.indexOf("n")+1);
successPage=successPage.substring(successPage.indexOf("n")+1);
successPage=successPage.substring(0,successPage.indexOf("n")-1);

}

String overWrite;

if (file.indexOf("name="OverWrite"")>0)
{
overWrite=file.substring(file.indexOf("name="OverWrite""));
overWrite=overWrite.substring(overWrite.indexOf("n")+1);
overWrite=overWrite.substring(overWrite.indexOf("n")+1);
overWrite=overWrite.substring(0,overWrite.indexOf("n")-1);

}
else
{
overWrite="false";
}
String overWritePage="";

if (file.indexOf("name="OverWritePage"")>0)
{
overWritePage=file.substring(file.indexOf("name="OverWritePage""));
overWritePage=overWritePage.substring(overWritePage.indexOf("n")+1);
overWritePage=overWritePage.substring(overWritePage.indexOf("n")+1);
overWritePage=overWritePage.substring(0,overWritePage.indexOf("n")-1);

}

String saveFile=file.substring(file.indexOf("filename="")+10);
saveFile=saveFile.substring(0,saveFile.indexOf("n"));
saveFile=saveFile.substring(saveFile.lastIndexOf("")+1,saveFile.indexOf("""));

int pos;
pos=file.indexOf("filename="");
pos=file.indexOf("n",pos)+1;
pos=file.indexOf("n",pos)+1;
pos=file.indexOf("n",pos)+1;

int boundaryLocation=file.indexOf(boundary,pos)-4;
file=file.substring(pos,boundaryLocation);
String fileName=new String(rootPath+directory+saveFile);
File checkFile=new File(fileName);
if (checkFile.exists())
{
if (!overWrite.toLowerCase().equals("true"))
{
if(overWritePage.equals(""))
{
out.println("Sorry the file already exists");
}
else
{
response.sendRedirect(overWritePage);
}
return;
}
}

File fileDir=new File(rootPath+directory);
if (!fileDir.exists())
{
fileDir.mkdirs();
}

fileOut=new FileOutputStream(fileName);
fileOut.write(file.getBytes(),0,file.length());

if(successPage.equals(""))
{
out.println("successMessage");
out.println("File Written to" +fileName);
}
else
{
response.sendRedirect(successPage);
}
}
else
{
out.println("Request not multipart/form-data");
}
}
catch(Exception e)
{
try
{
System.out.println("Error in doPost");
out.println("An Unexpected Error Occured");
out.println("Error Description" +e);
}
catch(Exception f)
{

}
}
finally
{
try
{
fileOut.close();
}
catch(Exception f)
{
}
try
{
in.close();
}
catch(Exception f)
{
}
try
{
out.close();
}
catch(Exception f)
{
}
}
}
}

Works fine for me...
Hope that helps

Ajay


<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by narensv:

Hi:
can anyone help me with the transfer of afile from one machine to another using servlets. I want a java program which when run from the dos prompt would transfer a file on my machine (location is hardcoded) to another machine and save it in a particular folder on that machine. both the machines are in the same network.

naren
[/quote]

Comments on this post
JimmyGosling agrees!
Gran Roguismo agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > file transfer in servlets

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap