Upload Image to FTP Server using ADF Mobile Application

744 Views Asked by At

I want to upload an image to FTP Server. Currently i am using JDeveloper 12c(12.1.3.0).

My Code:

private static final int BUFFER_SIZE = 4096;
public String fileUploadMethod(String imagePath){   
    String ftpUrl = "ftp://";
    String host = "http://192.168.0.42";
    String user = "XXXXXX";
    String pass = "XXXXXX";
    String filePath = "783771-1.jpg";
    String uploadPath = imagePath;
    ftpUrl =ftpUrl + user +":"+ pass+"@"+host+"/"+filePath+";";
    System.out.println("Upload URL: " + ftpUrl);

    try {
         URL url = new URL(ftpUrl);
         URLConnection conn = url.openConnection();
         OutputStream outputStream = conn.getOutputStream();
         FileInputStream inputStream = new FileInputStream(uploadPath);

         byte[] buffer = new byte[BUFFER_SIZE];
         int bytesRead = -1;
         while ((bytesRead = inputStream.read(buffer)) != -1) {
               outputStream.write(buffer, 0, bytesRead);
         }

         inputStream.close();
         outputStream.close();

         System.out.println("File uploaded");
               return "File uploaded";
         } catch (IOException ex) {
               ex.printStackTrace();
         }
   return null;
}

I am getting an error MalFormedURLException i.e. in detail message "unknown protocol:ftp"

Is there any other option to upload an image using JDeveloper.

Any idea regarding this.

Thanks, Siddharth

2

There are 2 best solutions below

0
On

Your ftpUrl is wrong. Remove http:// in the host variable. Should be ok then

2
On

I haven't really tried ftp upload. But I had tried with multipart form upload. As far as I know, MAF doesnt provide Out-Of-Box support for file upload. What I did was essential recreating the HTTP stream for the image upload.

The POC code is attached below. This may be definitely the CRUDEST implementation but I am not sure if there is a better way.

public void doUpload() {
        try {
            DeviceManager dm = DeviceManagerFactory.getDeviceManager();
            String imgData =
                dm.getPicture(50, DeviceManager.CAMERA_DESTINATIONTYPE_FILE_URI, DeviceManager.CAMERA_SOURCETYPE_CAMERA,
                              false, DeviceManager.CAMERA_ENCODINGTYPE_PNG, 0, 0);
            imgData = imgData.substring(7, imgData.length());
            int start = imgData.lastIndexOf('/');
            String fileName = imgData.substring(start+1, imgData.length());
            RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
            restServiceAdapter.clearRequestProperties();
            String requestMethod = RestServiceAdapter.REQUEST_TYPE_POST;
            String requestEndPoint = restServiceAdapter.getConnectionEndPoint("serverBaseUrl");
            String requestURI = "/workers/100000018080264";
            String request = requestEndPoint + requestURI;
            HashMap httpHeadersValue = new HashMap();
            httpHeadersValue.put("X-ANTICSRF", "TRUE");
            httpHeadersValue.put("Connection", "Keep-Alive");
            httpHeadersValue.put("content-type","multipart/form-data; boundary=----------------------------4abf1aa47e18");
            // Get the connection
            HttpConnection connection = restServiceAdapter.getHttpConnection(requestMethod, request, httpHeadersValue);
            OutputStream os = connection.openOutputStream();
            byte byteBuffer[] = new byte[50];
            int len;
            //String temp is appended before the image body
            String temp = "------------------------------4abf1aa47e18\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" +fileName+ "\"\r\nContent-Type: image/jpeg\r\n\r\n";
            InputStream stream = new ByteArrayInputStream(temp.getBytes("UTF-8"));
            if (stream != null) {
                while ((len = stream.read(byteBuffer)) >= 0) {
                    os.write(byteBuffer, 0, len);
                }
                stream.close();
            }
            FileInputStream in = new FileInputStream(imgData);
            if (in != null) {
                while ((len = in.read(byteBuffer)) >= 0) {
                    os.write(byteBuffer, 0, len);
                }
                in.close();
            }
            //The below String is appended after the image body
            InputStream stream2 =new ByteArrayInputStream("\r\n------------------------------4abf1aa47e18--\r\n".getBytes("UTF-8"));
            if (stream2 != null) {
                while ((len = stream2.read(byteBuffer)) >= 0) {
                    os.write(byteBuffer, 0, len);
                }
                stream2.close();
            }
            int status = connection.getResponseCode();
            InputStream inputStream = restServiceAdapter.getInputStream(connection);
            ByteArrayOutputStream incomingBytes = new ByteArrayOutputStream()           // get and process the response.
            while ((len = inputStream.read(byteBuffer)) >= 0) {
                incomingBytes.write(byteBuffer, 0, len);
            }
            String ret = incomingBytes.toString();
            incomingBytes.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }