繁体   English   中英

如何在Android中上传图片?

[英]How to upload image in android?

服务器代码

 @POST
     @Path("/feedback1")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     @Produces(MediaType.APPLICATION_JSON)
     public ServiceResponse Feedback1(@FormDataParam("file") InputStream file_in,@FormDataParam("file") FormDataContentDisposition fileDetail)
     {
      final ServiceResponse response = new ServiceResponse();
      Boolean flag=false;int size = 0;
      String fileName = null;
      fileName = fileDetail.getFileName().trim();
      try{
       File file = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +fileDetail.getFileName());
       FileOutputStream fos = new FileOutputStream(file);
       int read =0;
       byte[] bytes = new byte[1024];
       while ((read = file_in.read(bytes)) != -1) {
        fos.write(bytes, 0, read);
       }
       size=(int) (file.length()/1024);
       System.out.println(size);

      }catch(IllegalStateException | IOException e)
      {
       LOG.error("problem sending feedback from Feedback method : ", e);
       flag=true;
       ServiceUtil.setFailureMessage(response, e.getMessage(), null);
      }
      if (!flag) {
       ServiceUtil.setSuccessMessage(response, SUCCESS +" filename= "+ fileName + " size =" + size +"KB", null);
      }
      return response;
     }

你好

我正在服务器上上传图像。我能够获取图像的字节数。现在我需要在服务器上上传该图像。能告诉我如何实现此目标吗?

java.net.ProtocolException:不支持输出

这是我的代码。我在该函数中传递了图像的字节。但是我在这行上遇到了错误

          ****dos = new DataOutputStream(conn.getOutputStream());****

private void uploadFile(byte[] bytes) {
    // TODO Auto-generated method stub
     HttpURLConnection conn = null;
     DataOutputStream dos = null;  
     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary = "*****";
      try {
        URL url = new URL("http://192.XXX.X2.X:8080/FGRailApps/jservices/rest/feedback1");

          // Open a HTTP  connection to  the URL
          conn = (HttpURLConnection) url.openConnection(); 
          conn.setUseCaches(false); // Don't use a Cached Copy
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Connection", "Keep-Alive");
          conn.setRequestProperty("ENCTYPE", "multipart/form-data");
          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

          dos = new DataOutputStream(conn.getOutputStream());

          dos.writeBytes(twoHyphens + boundary + lineEnd); 
          dos.writeBytes("Content-Disposition: form-data; name=naveen;filename= test"  + lineEnd);

          dos.writeBytes(lineEnd);
          dos.write(bytes);
          dos.writeBytes(lineEnd);
          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

          // Responses from the server (code and message)
          serverResponseCode = conn.getResponseCode();
          String serverResponseMessage = conn.getResponseMessage();
          Log.d("---------------", serverResponseMessage);

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 



}
public static boolean Upload(String sourceFileName) {
    String uuid = UUID.randomUUID().toString();
    System.out.println("uuid = " + uuid);
    FTPClient ftpClient = new FTPClient();
    // File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sourceFileName);       
    try {
        ftpClient.connect(InetAddress.getByName(FTPCONSTANTS.HOST_NAME));
        ftpClient.login(FTPCONSTANTS.USER_NAME, FTPCONSTANTS.PASSWORD);
        ftpClient.changeWorkingDirectory(MessageActivity.Path1);    

        //if (ftpClient.getReplyString().contains("250")) {
            Log.e("%%%%%%%%%", "888888888");

            ftpClient
                    .setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            BufferedInputStream buffIn = null;
            ftpClient.enterLocalPassiveMode();
            FileInputStream in = new FileInputStream(file);




                result = ftpClient.storeFile(MessageActivity.Path1 + uuid
                        + "." + "mp4", in);

                result1 = MessageActivity.Path1 + uuid + "." + "mp4";
                System.out.println("cjkkkkkkkkkkkkkkkk " + result1);






            /*
             * result = ftpClient.storeFile( FTPCONSTANTS.FOLDER_PATH + "/"
             * + sourceFileName.substring( sourceFileName.lastIndexOf("/") +
             * 1, sourceFileName.length()), in);
             */
            in.close();
            if (result)
                Log.v("upload result", "succeeded");
            ftpClient.logout();
            ftpClient.disconnect();
        //}

    } catch (SocketException e) {
        Log.e("", e.getStackTrace().toString());
    } catch (UnknownHostException e) {
        Log.e("", e.getStackTrace().toString());
    } catch (IOException e) {
        Log.e("", e.getStackTrace().toString());
    }
    return result;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM