473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to write a file to a different pc

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..
Jul 18 '07 #1
2 1393
JosAH
11,448 Expert 8TB
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
Jul 18 '07 #2
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....
Jul 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
16
by: btopenworld | last post by:
Hi - question from a relative asp novice I have written to text files in the past, but always appending new data to the end of the text file. I now want to add the new data to the beginning of...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: eholz1 | last post by:
Hello PHP Group, I am having trouble setting permissions correctly so that the magickwand api (php 5.2) can read and write images. I usually read a file from one directory, create a magickwand...
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
0
by: Leo Jay | last post by:
I'd like to read and write the same socket in different threads. one thread is only used to read from the socket, and the other is only used to write to the socket. But I always get a 10022...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.