【问题标题】:how to upload picture file to mysql from android using Restful webservice如何使用Restful webservice将图片文件从android上传到mysql
【发布时间】:2014-06-16 06:49:34
【问题描述】:

我想使用 RESTful 服务将图像从 android 应用程序上传到 mysql 数据库。 是他们的任何服务端和android端教程吗?请帮助提供RESTful和android示例

提前致谢

【问题讨论】:

    标签: android android-webservice


    【解决方案1】:

    这个Tutorial 将帮助上传图片文件和

    tutorial 将帮助您将图像写入数据库。

    添加部分代码:

    文件上传网络服务:

    @Path("/file")
    public class UploadFileService {
    
        @POST
        @Path("/upload")
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {
    
            String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();
    
            // save it
            writeToFile(uploadedInputStream, uploadedFileLocation);
    
            String output = "File uploaded to : " + uploadedFileLocation;
    
                /************************************************/ 
                 // CALL THE IMAGE UPLOAD TO DB CODE HERE.
                 // InsertImageTest.insertImage();
                /*************************************************/
            return Response.status(200).entity(output).build();
    
        }
    
        // save uploaded file to new location
        private void writeToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {
    
            try {
                OutputStream out = new FileOutputStream(new File(
                        uploadedFileLocation));
                int read = 0;
                byte[] bytes = new byte[1024];
    
                out = new FileOutputStream(new File(uploadedFileLocation));
                while ((read = uploadedInputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                out.close();
            } catch (IOException e) {
    
                e.printStackTrace();
            }
    
        }
    
    }
    

    将图像保存到数据库的 JAVA 代码

    public class InsertImageTest {
    
        /**
         * This is used to get the Connection
         * 
         * @return
         */
        public Connection getConnection() {
            Connection connection = null;
    
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/technicalkeeda", "root", "");
            } catch (Exception e) {
                System.out.println("Error Occured While Getting the Connection: - "
                        + e);
            }
            return connection;
        }
    
        /**
         * Insert Image
         */
        public void insertImage() {
            Connection connection = null;
            PreparedStatement statement = null;
            FileInputStream inputStream = null;
    
            try {
                File image = new File("C:/honda.jpg");
                inputStream = new FileInputStream(image);
                connection = getConnection();
                statement = connection
                        .prepareStatement("insert into trn_imgs(img_title, img_data) "
                                + "values(?,?)");
                statement.setString(1, "Honda Car");
                statement.setBinaryStream(2, (InputStream) inputStream,
                        (int) (image.length()));
    
                statement.executeUpdate();
            } catch (FileNotFoundException e) {
                System.out.println("FileNotFoundException: - " + e);
            } catch (SQLException e) {
                System.out.println("SQLException: - " + e);
            } finally {
                try {
                    connection.close();
                    statement.close();
                } catch (SQLException e) {
                    System.out.println("SQLException Finally: - " + e);
                }
            }
    
        }
    
        /***
         * Execute Program
         * 
         * @param args
         * @throws SQLException
         */
        public static void main(String[] args) throws SQLException {
            InsertImageTest imageTest = new InsertImageTest();
            imageTest.insertImage();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      将任何文件从 android 上传到 Mysql 服务器的最佳且简单的方法是使用 FTP 客户端库。 Link

      找到ftp4j-1.6.jar 文件并导入到您的项目中

      然后您可以通过以下代码将图像上传到您的服务器。

      public void upload(){
      
      
          FTPClient con = null;
      
      
              con = new FTPClient();
              con.connect("192.168.2.57"); // Your Server IP and Port you can use FTP domain credentials here also
      
              if (con.login("XXXXXXXXX", "XXXXX")) // FTP username and Pass
              {
                  con.enterLocalPassiveMode(); // important!
                  con.setFileType(FTP.BINARY_FILE_TYPE);
                  String data = "/sdcard/vivekm4a.m4a"; // Your File Path
      
                  FileInputStream in = new FileInputStream(new File(data));
                  boolean result = con.storeFile("/vivekm4a.m4a", in);
                  in.close();
                  if (result) Log.v("upload result", "succeeded");
                  con.logout();
                  con.disconnect();
              }
          }
      
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 2015-01-31
        • 2011-06-30
        • 2018-02-22
        • 2012-06-11
        相关资源
        最近更新 更多