wood burning stoves 2.0*
The moose likes Java in General and the fly likes to upload a file to a remote server on the network Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "to upload a file to a remote server on the network" Watch "to upload a file to a remote server on the network" New topic
Author

to upload a file to a remote server on the network

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
how do make the server available for a file transfer over the network using code similar to this:
Got this code on the internet by the way the author is Muthu.
Right now I get an error message:




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41101
    
  45
I'd advise to use a proper FTP client library like Apache Commons Net that takes care of all the details of the FTP protocol.


Ping & DNS - my free Android networking tools app
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Thanks, How do I prepare the server to be an Ftp server?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The code you posted looked like FTP client code to me. That would be code which uploads to an FTP server which is already running. Are you now saying you don't have a running FTP server yet?

If that's the case, then you get one and install it. That's a completely separate operation from writing code to upload data to it, and you can't use client code to create an FTP server or to start one. But perhaps I misunderstood that last question?
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Sorry I was not clear. I am not sure how to prepare a linux machine to be a ftp server. In other words I want to use the code I posted to access the ftp server. Does that make sense?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Sonia:

Try googling "linux ftp server setup". I was able to find some information that might be helpful.

John.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

sonia pandit wrote:I am not sure how to prepare a linux machine to be a ftp server. In other words I want to use the code I posted to access the ftp server. Does that make sense?


No, it doesn't. At least, not the phrase "in other words" which implies the two statements somehow mean the same thing.

To prepare a machine to be an FTP server, you install some FTP server software on it and configure it appropriately. That only has to be done once. After that has been done -- and probably somebody in your organization has already done it -- then you can use code like what you posted to access it. This can be done as many times as necessary.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I found a free perl script to use for uploading a file to the remote server at:

http://cgi.resourceindex.com/Programs_and_Scripts/Perl/File_Management/File_Uploading/

However, they do not seem to have a perl file to download a file from the remote server.

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

how can i upload file on remote server using http with java programming
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

http://commons.apache.org/proper/commons-fileupload/using.html


Swastik
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Megha Singhal wrote:how can i upload file on remote server using http with java programming


Try reading this thread, since that's what it's about.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

alongwith following jsp i try to upload file using servlet

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<form method="post" action="UploadServlet" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>

save as upload.jsp

and then following servlet


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private static final String DATA_DIRECTORY = "resources";
private static final int MAX_MEMORY_SIZE = 1024 * 1024 * 2;
private static final int MAX_REQUEST_SIZE = 1024 * 1024;

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
doPost(req, res);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}


String filePath =null;
String fileName =null;
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(MAX_MEMORY_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
HttpSession session=request.getSession();
String uploadFolder = getServletContext().getRealPath("")+ File.separator + DATA_DIRECTORY;
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_REQUEST_SIZE);
try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = new File(item.getName()).getName();
filePath = uploadFolder + File.separator + fileName;
File uploadedFile = new File(filePath);
item.write(uploadedFile);
}
}

} catch (FileUploadException ex) {
throw new ServletException(ex);
}
catch (Exception ex) {
throw new ServletException(ex);
}
session.setAttribute("path",fileName);
request.getRequestDispatcher("/done.jsp").forward(request,response);

}
}



now i am getting following error

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Wrapper cannot find servlet class UploadServlet or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
root cause

java.lang.ClassNotFoundException: UploadServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

Apache Tomcat/6.0.16


kindly help me.. i already set the path jar files like servlet api, commons io and java.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Megha,

Can you please show us the directory structure of your web application and the web.xml file.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

Swastik Dey wrote:Megha,

Can you please show us the directory structure of your web application and the web.xml file.


yes sure..

first i made folder named it "upload" and within that folder i made another folder named "WEB-INF" and along with that folder i placed my jsp files their and within WEB-INF folder i made two folders named it "classes" in which i put my servlet file and another folder named "lib" that folder first i kept empty then i put commons.io jar files for uploading in that. also i already set path for the jar files in environmental variables.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Now show us the web.xml file please.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

Swastik Dey wrote:Now show us the web.xml file please.


xml code

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>upload</display-name>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>UploadServlet</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
</web-app>
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Megha,

Put the servlet class inside a package, put the package inside WEB-INF\classes, and change the mapping in web.xml accordingly.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
> put the package inside WEB-INF\classes

Or if the package is a jar file, then into WEB-INF/lib
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

Swastik Dey wrote:Megha,

Put the servlet class inside a package, put the package inside WEB-INF\classes, and change the mapping in web.xml accordingly.


you are talking about which package?.. i didn't understand.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

You should have your own package that contains the .java file of servlet. For e.g.

package megha;
public class UploadServlet{
//your code goes here.
}

//you should read about java packages.
http://www.tutorialspoint.com/java/java_packages.htm

In simple language it's a sort of a folder where the .class file gets stored. So in your case there should be a folder called, megha that contains the .class file for UploadServlet. You should now copy this megha folder to
WEB-INF\classes, and change the mapping in web.xml

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

Swastik Dey wrote:You should have your own package that contains the .java file of servlet. For e.g.

package megha;
public class UploadServlet{
//your code goes here.
}

//you should read about java packages.
http://www.tutorialspoint.com/java/java_packages.htm

In simple language it's a sort of a folder where the .class file gets stored. So in your case there should be a folder called, megha that contains the .class file for UploadServlet. You should now copy this megha folder to
WEB-INF\classes, and change the mapping in web.xml



i understand what are packages but the i just wanted to know why we need to create packages.
it just a simple program that i want to run on tomcat.
package we usually make when we need to reuse it somewhere.
so can you please explain me why you you are suggesting me to use package here?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

In the following post read the comments made by William Brogden

http://www.coderanch.com/t/545423/Tomcat/Unable-load-servlet-Tomcat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to upload a file to a remote server on the network
 
Similar Threads
Show FTP log in JTextField
error outputting a pdf file
Generating PDF and displaying it ...
How to read a file from remote System
Servlet downloads Excel file ok but also attempts Servlet & JSP files !!