我一直在嘗試幾天來做到這一點,並得到絕對無處。我知道這是可以做到的,但我一直在尋找答案,沒有任何工作。插入圖像到MySQL數據庫
- 用我的REST客戶端
- 插入的是上傳圖片到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。 我可以把它作爲一個文件鏈接,只是加載在我的網站上的圖像,但我甚至無法得到那麼多。
你確定把圖像放入數據庫是個好主意嗎?其次,你的模式是什麼?第三是你用什麼代碼來插入? – tadman 2015-03-02 21:33:16