2015-03-02 233 views
0

我一直在尝试几天来做到这一点,并得到绝对无处。我知道这是可以做到的,但我一直在寻找答案,没有任何工作。插入图像到MySQL数据库

  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。 我可以把它作为一个文件链接,只是加载在我的网站上的图像,但我甚至无法得到那么多。

+0

你确定把图像放入数据库是个好主意吗?其次,你的模式是什么?第三是你用什么代码来插入? – tadman 2015-03-02 21:33:16

回答

0

我建议将您的图像存储在某种便宜且具有良好权限的平面存储(如网络存储)中,然后在数据库中存储该存储位置的路径。如果你将图像存储为blob,那么数据库本来会做类似的事情,但我相信在数据库管理存储和检索这些图像方面会有一些开销。这些映像会占用大量数据库的磁盘空间,如果要为映像添加更多空间,向平面存储添加空间应该比为数据库增加空间更容易。