【发布时间】:2015-03-02 21:06:36
【问题描述】:
我已经尝试了几天来做到这一点,但完全没有成功。我知道可以做到,但我一直在寻找答案,但没有任何效果。
- 使用我的 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。 我可以将其设为文件链接,然后将图片加载到我的网站上,但我什至还达不到那一步。
【问题讨论】:
-
您确定将图像放入数据库是个好主意吗?其次,你的架构是什么?三是你用什么代码插入?