Results 1 to 9 of 9

Thread: how to upload file to ftp server

  1. #1
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default how to upload file to ftp server

    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?

  2. #2
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default

    no suggestions?

  3. #3

    Default

    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.

  4. #4
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default

    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:
    Code:
    <%@ 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;
    
        if(isMultipart){
            int tamaoMaximoArchivo = 1024*1024*10; //valor en bytes --> (10MB)
            String nombreArchivo = NameRandom;
            DiskFileItemFactory diskFile = new DiskFileItemFactory();
            diskFile.setSizeThreshold(tamaoMaximoArchivo);
            FileItemFactory factory = diskFile;
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(request); 
            if(items.size()>0){
                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);
                    try{
                        int PIECE_SIZE = 25000; //tamao de transferencia (56 Kbps = 7KB)
                        int bytes = 0; 
                        byte[] piece = new byte[PIECE_SIZE];
                        String server = "190.81.99.99";// 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) {
                                break;
                            } 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);
                                }
                            }
                        }
                        os.close(); 
                        fis.close(); 
                        result = "{success: true}";
                    }catch(Exception e){
                        JOptionPane.showMessageDialog(null, e);
                        result = "{success: false}";
                    }
                }
            }
        }
        //result = "{success: true}";
    %>
    
    <%=result %>

  5. #5
    Sencha User hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,966

    Default

    @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.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  6. #6
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default

    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

  7. #7
    Touch Premium Member
    Join Date
    Feb 2008
    Posts
    206

    Default

    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

  8. #8
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default

    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

  9. #9
    Sencha User
    Join Date
    Jul 2009
    Posts
    86

    Default

    Can anyboy help me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •