View Full Version : how to upload file to ftp server

23 Nov 2009, 2:47 PM
hi all, I've got a question about upload files to FTP server, well I upload one file from my PC to the FTP server and works OK, (the application is in my pc), but If I access to my page from other PC I can't upload to the FTP, can anybody help me please?

24 Nov 2009, 7:15 AM
no suggestions?

Mike Robinson
24 Nov 2009, 8:14 AM
No information. :-?

You say nothing about where the FTP server is (is it "your PC?"), nor what kind of server, nor anything else that could possibly be used to offer a solution.

24 Nov 2009, 8:41 AM
well, the ftp server is other pc, and from my pc, I have the code to upload the file to ftp, so when I run the webapp, I can upload to the ftp, but from another pc I put the url from my pc, and I can upload

and the code for upload to ftp is this:

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.swing.JOptionPane.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLConnection" %>


boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String result="";
String fieldName = null;
String fileName = null;
String NameRandom = java.util.UUID.randomUUID().toString();
String format = null;
long sizeInBytes = 0;

int tamañoMaximoArchivo = 1024*1024*10; //valor en bytes --> (10MB)
String nombreArchivo = NameRandom;
DiskFileItemFactory diskFile = new DiskFileItemFactory();
FileItemFactory factory = diskFile;
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
FileItem item = (FileItem) items.get(1);
if (!item.isFormField()) {
fieldName = item.getFieldName();
fileName = item.getName();
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
format =fileName.substring(fileName.length()-4);
int PIECE_SIZE = 25000; //tamaño de transferencia (56 Kbps = 7KB)
int bytes = 0;
byte[] piece = new byte[PIECE_SIZE];
String server = "";// The ftp server
String user = "user";//ftp user
String password = "pass";//ftp pass

String remoteFile = "/hola/"+nombreArchivo+format;
String strUrl = "ftp://" + user + ":" + password + "@" + server + remoteFile + ";type=i";
URL url = new URL(strUrl);
URLConnection urlc = url.openConnection();
FileInputStream fis = new FileInputStream(new File(item.getName()));
OutputStream os = urlc.getOutputStream();
while (true) {
bytes = fis.available();
if (bytes <= 0) {
} else {
if (bytes >= PIECE_SIZE) {//PIECE_SIZE
fis.read(piece, 0, piece.length);
os.write(piece, 0, piece.length);
} else {
fis.read(piece, 0, bytes);
os.write(piece, 0, bytes);
result = "{success: true}";
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
result = "{success: false}";
//result = "{success: true}";

<%=result %>

24 Nov 2009, 8:48 AM
@Andryi -- Do some research on the browsers' "same-origin" policy. That is likely your problem. Requests can usually only be sent to the same server that served up the HTML Page.

24 Nov 2009, 8:58 PM
hi hendricd, well I read some files about "same-origin", and until I understand this problem is because the security of each browser is restrict the origin of the file to upload,so when I upload a file from another pc, never have the real origin path file, so how can configure that, I read that It's needs sign javascript but I don't think

24 Nov 2009, 9:20 PM
You could handle this on the server.

1) Upload the file from the client PC to the web server using HTTP
2) Upon success result, fire off an ajax request to have the server ftp the file somewhere
3) monitor the ftp process from the web browser by polling the server for status

26 Nov 2009, 7:30 AM
well I can upload file by HTTP, and If I do a bridge between http with the ftp, I don't think this the best idea , because I want to upload directly to the ftp

30 Nov 2009, 6:26 AM
Can anyboy help me?