gpt4 book ai didi

java - 将图像插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 12:22:28 25 4
gpt4 key购买 nike

我已经尝试了好几天了,但毫无进展。我知道这是可以做到的,但我一直在寻找答案,但没有任何效果。

  1. 使用我的 REST 客户端上传图片
  2. 将上传的图片插入 MySQL 数据库。

我尝试过的:正在关注Load_File doesn't work ,我使用的是 OS X,所以我不知道如何更改文件夹的所有权等...我该怎么做?我在上一篇文章中从未得到过关于此问题的答案。我该怎么做?

我也尝试过另一种方式:http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/

这根本不起作用。我不断收到这篇文章中描述的错误:Jersey REST WS Error: "Missing dependency for method... at parameter at index X" ,但答案对我没有帮助,因为我仍然不知道它应该是什么......

有人可以指导我吗?

我正在使用 Java 中的 Jersey REST 客户端。许多教程都提到了 pom.xml 文件,但我没有,也不知道它是什么。

谢谢你,奥马尔

编辑:这是文件上传:

package com.omar.rest.apimethods;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;


@Path("/files")
public class FileUpload {

private String uploadLocationFolder = "/Users/Omar/Pictures/";

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

String filePath = "/Users/Omar/Pictures/" + contentDispositionHeader.getFileName();

// save the file to the server
saveFile(fileInputStream, filePath);

String output = "File saved to server location : " + filePath;

return Response.status(200).entity(output).build();

}

// save uploaded file to a defined location on the server
private void saveFile(InputStream uploadedInputStream,
String serverLocation) {

try {
OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];

outpuStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch (IOException e) {

e.printStackTrace();
}

}

}

My Jar List

表的架构(我为测试而创建的架构):image_id:int自增PK,图片:BLOB。我可以将其设为文件链接,然后将图像加载到我的网站上,但我什至还无法做到这一点。

最佳答案

我建议将您的图像存储在某种廉价的、权限良好的平面存储中,例如网络存储,然后在数据库中存储该存储位置的路径。如果您将图像存储为 blob,数据库无论如何都会执行与此类似的操作,但我相信让数据库管理存储和检索这些图像会涉及一些开销。这些图像将占用数据库的大量磁盘空间,如果您想为图像添加更多空间,向平面存储添加空间应该比向数据库添加空间更容易。

关于java - 将图像插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819423/

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