1

我想将图像上传到 FTP 服务器。目前我正在使用 JDeveloper 12c(12.1.3.0)。

我的代码:

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即详细消息“未知协议:ftp”

是否有任何其他选项可以使用 JDeveloper 上传图像。

关于这个的任何想法。

谢谢,悉达多

4

2 回答 2

0

我还没有真正尝试过ftp上传。但我曾尝试使用多部分表单上传。据我所知,MAF 不提供文件上传的开箱即用支持。我所做的是为图像上传重新创建 HTTP 流。

POC 代码附在下面。这可能绝对是最简单的实现,但我不确定是否有更好的方法。

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();
        }
    }
于 2014-07-25T06:30:53.040 回答
0

ftpUrl错了。http://在宿主变量中删除。那应该没问题

于 2014-07-24T17:15:18.543 回答