2015-03-02 141 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,那麼數據庫本來會做類似的事情,但我相信在數據庫管理存儲和檢索這些圖像方面會有一些開銷。這些映像會佔用大量數據庫的磁盤空間,如果要爲映像添加更多空間,向平面存儲添加空間應該比爲數據庫增加空間更容易。