2014-07-24 5 views
1

FTP 서버에 이미지를 업로드하고 싶습니다. 현재 JDeveloper 12c (12.1.3.0)를 사용하고 있습니다.ADF Mobile 응용 프로그램을 사용하여 FTP 서버에 이미지 업로드

내 코드 : JDeveloper를 사용하여 이미지를 업로드 할 수있는 다른 옵션이 있습니까

"ftp를 알 수없는 프로토콜"

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; 
} 

나는 세부 메시지에서 오류를 MalformedURLException가 즉를 얻고있다.

이 문제와 관련하여 알려주십시오.

감사합니다, 싯다 르트 나라 얀

답변

0

귀하의 ftpUrl은 잘못된 것입니다. 호스트 변수에서 http://을 제거하십시오. 좋아, 그럼

+0

http : //를 제거했지만 여전히 작동하지 않습니다. – Siddh

0

나는 정말 FTP 업로드를 시도하지 않았습니다. 그러나 다중 파트 양식 업로드를 시도했습니다. 내가 아는 한, MAF는 파일 업로드에 대한 Out-Of-Box 지원을 제공하지 않습니다. 내가 한 것은 이미지 업로드를 위해 HTTP 스트림을 다시 만드는 것입니다.

아래에 POC 코드가 첨부되어 있습니다. 확실히 CRUDEST 구현 일 수 있지만 더 좋은 방법이 있는지 확실하지 않습니다.

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(); 
     } 
    } 
+0

FTP 연결과 HTTP 연결이 다릅니다. FTP로만 업로드하고 싶습니다. – Siddh

+0

서버가 MultiPart, 즉 Http Connection을 사용한 이미지 업로드를 지원하지 않기 때문에 ... – Siddh