【问题标题】:Insert image into MySQL database将图像插入 MySQL 数据库
【发布时间】:2015-03-02 21:06:36
【问题描述】:

我已经尝试了几天来做到这一点,但完全没有成功。我知道可以做到,但我一直在寻找答案,但没有任何效果。

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

}

}

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

【问题讨论】:

  • 您确定将图像放入数据库是个好主意吗?其次,你的架构是什么?三是你用什么代码插入?

标签: java mysql rest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 2018-05-23
    相关资源
    最近更新 更多