gpt4 book ai didi

android - 如何使用Restful webservice从android上传图片文件到mysql

转载 作者:太空狗 更新时间:2023-10-29 15:03:34 26 4
gpt4 key购买 nike

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

提前致谢

最佳答案

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

}

关于android - 如何使用Restful webservice从android上传图片文件到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24238009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com