Connecting Tech Pros Worldwide Forums | Help | Site Map

how to write a file to a different pc

Newbie
 
Join Date: Jul 2007
Posts: 9
#1: Jul 18 '07
I am doing a file upload program...
here I have my program running on tomcat server ..
when the file is to be uploaded I create a FILE object pointing to a folder on that server and upload it .. this works fine..

BUT now i have to store that file on some other server..

I have ip of that server ...

HOw can i write to another server....

the other part of problem is that my first server is linux and second is on windows 2000..

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jul 18 '07

re: how to write a file to a different pc


Quote:

Originally Posted by abhishek92

I am doing a file upload program...
here I have my program running on tomcat server ..
when the file is to be uploaded I create a FILE object pointing to a folder on that server and upload it .. this works fine..

BUT now i have to store that file on some other server..

I have ip of that server ...

HOw can i write to another server....

the other part of problem is that my first server is linux and second is on windows 2000..

At least you have to conform to the protocol *how* that other server wants you
to upload your file.

kind regards,

Jos
Newbie
 
Join Date: Jul 2007
Posts: 9
#3: Jul 19 '07

re: how to write a file to a different pc


Quote:

Originally Posted by JosAH

At least you have to conform to the protocol *how* that other server wants you
to upload your file.

kind regards,

Jos

my code is here:::----

Expand|Select|Wrap|Line Numbers
  1. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>  
  2. <%@ page import="java.util.List"%>  
  3. <%@ page import="java.util.Iterator"%>  
  4. <%@ page import="java.io.File"%>
  5. <%@ page import="org.apache.commons.fileupload.*"%>  
  6. <%@ page import="org.apache.commons.fileupload.disk.*"%>  
  7. <%@ page import="org.apache.commons.fileupload.servlet.*"%>  
  8.  
  9. <html>  
  10. <head>  
  11. <title>Commons-FileUpload-1.1 Report Page</title>  
  12. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
  13. </head>  
  14.  
  15. <%!      
  16.     //method to return file extension  
  17.     String getFileExt(String xPath)
  18.     {  
  19.         //Find extension  
  20.         int dotindex = 0;    //extension character position  
  21.         dotindex = xPath.lastIndexOf('.');  
  22.         if (dotindex == -1) // no extension      
  23.         {    
  24.                 return "";  
  25.         }  
  26.         int slashindex = 0;    //seperator character position  
  27.         slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));  
  28.         if (slashindex == -1)    // no seperator characters in string  
  29.         {
  30.             return xPath.substring(dotindex);  
  31.         }  
  32.         if (dotindex < slashindex)    //check last "." character is not before last seperator  
  33.         {    
  34.             return "";  
  35.         }  
  36.         return xPath.substring(dotindex);  
  37.     }
  38.  
  39. %>  
  40. <body>
  41.  
  42. <%  
  43.     //Create variables for path, filename and extension  
  44.     String newFilePath = application.getRealPath("/") + "fileup2\\upload";
  45.     String newFileName ="";  
  46.     String FileExt = "";  
  47.     // Create a factory for disk-based file items  
  48.     FileItemFactory factory = new DiskFileItemFactory();  
  49.     // Create a new file upload handler  
  50.     ServletFileUpload upload = new ServletFileUpload(factory);  
  51.     // Parse the request  
  52.     List /* FileItem */ items = upload.parseRequest(request);  
  53.     // Process the uploaded items  
  54.     Iterator iter = items.iterator();  
  55.     //Form fields   
  56.     while (iter.hasNext())
  57.     {  
  58.         FileItem item = (FileItem) iter.next();  
  59.         if (item.isFormField())
  60.         {  
  61.             String name = item.getFieldName();  
  62.             String value = item.getString();  
  63.             if (name.equals("newFileName"))
  64.                 newFileName = value;  
  65.             out.println("Form Field Name= " + name + "<br>");  
  66.             out.println("Form Field Value= " + value + "<br><br>");  
  67.         }   
  68.         else
  69.         {  
  70.             String fieldName = item.getFieldName();  
  71.             String fileName = item.getName();  
  72.             FileExt = getFileExt(fileName);  
  73.             String contentType = item.getContentType();  
  74.             boolean isInMemory = item.isInMemory();  
  75.             long sizeInBytes = item.getSize();  
  76.             out.println("fieldName= " + fieldName + "<br>");  
  77.             out.println("fileName= " + fileName + " ");  
  78.             out.println("FileExt= " + FileExt + "<br>");  
  79.             out.println("contentType= " + contentType + "<br>");  
  80.             out.println("isInMemory= " + isInMemory + "<br>");  
  81.             out.println(" " + sizeInBytes + "Bytes <br>");  
  82.             if (fileName.equals("") || sizeInBytes==0)
  83.             {
  84.                 out.println("Not a valid file "+fileName+"("+sizeInBytes+"bytes) No upload attempted.<br>");
  85.                 return;
  86.             }      
  87.             else
  88.             {  
  89.                 if(sizeInBytes>5300000)
  90.                 {
  91.                     out.println("The size of file "+fileName+"("+sizeInBytes+"bytes) exceeds the limit !!! (LIMIT 5 MB)");
  92.                     return;                
  93.                 }
  94.                 else
  95.                 {
  96.                     File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);  
  97.                     try
  98.                     {
  99.                         item.write(uploadedFile);
  100.                         out.println(fileName+"("+sizeInBytes+"bytes) was successfully uploaded as "+ newFileName+FileExt +")");  
  101.                     }
  102.                     catch (java.lang.Exception e)
  103.                     {  
  104.                         out.println("Errors prevented the file upload. "+fileName+ " was not uploaded!!! Exception is :  "+e);
  105.                     }
  106.                 }  
  107.             }  
  108.         }  
  109.     }  
  110. %>  
  111. </body>  
  112. </html>  


the supporting html code is



Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  5. <title>file upload</title>
  6. </head>
  7.  
  8. <body>  
  9. <form method="post" action="Process_FileUpload.jsp" name="uploadform" enctype="multipart/form-data">    
  10.     <table >  
  11.     <tr>    
  12.         <td>
  13.             Select the file to upload
  14.         </td>    
  15.     </tr>
  16.     <tr>
  17.         <td>
  18.             Subject : <input type=text name=subject>        
  19.         </td>
  20.     </tr>    
  21.      <tr>    
  22.       <td> 
  23.           <input type="hidden" name="newFileName" value="newUploadedFileName1">    
  24.         <input type="file" name="uploadfile">    
  25.         </td>    
  26.     </tr>      
  27.     <tr>    
  28.         <td align="center">   
  29.             <input type="submit" name="Submit" value="Upload" >    
  30.             <input type="reset" name="Reset" value="Cancel">    
  31.         </td>    
  32.     </tr>    
  33.   </table>      
  34. </form>    
  35.  
  36. </body>  
  37. </html>

this code is running on one server ...
now you can see in code I have created a FILE class object that points on that server itself... and then item.write(<file class object>) is used to write...

but Is there any way that I can make a FILE class object to point to a location on some remote machine.....
how to do?????
can you suggest the protocol as you were asking....
Reply