[英]Java EE - Upload image to database?
我正在為我的母親創建一個相當基本的站點,並且看到上學期在Java EE和EJB中做了一些工作時,我會堅持下去。
我唯一的問題是上傳圖片-我似乎找不到任何示例。
我正在使用實體類和參數化查詢。 這是用於寫入數據庫的代碼,效果很好,我現在只是將blob圖像值設置為null。
@Override
public void addClothes(String designer, String cname, String ctype, String desc) {
Clothes c = new Clothes();
em.persist(c);
c.setDesigner(designer);
c.setCname(cname);
c.setCtype(ctype);
c.setDescript(desc);
c.setImage(null);
}
我有一個接收文件的servlet,但我不確定該文件在傳遞時應如何排序以及應寫入數據庫的內容(從我看到的內容來看,它是byte [])。
朝正確方向伸出的手將不勝感激!
將文件放在服務器上的內存中或本地或臨時文件中(取決於所使用的框架或庫)之后,您將擁有包裝類型的引用。
如果您正在使用Apache Commons File Upload ,則有FileItem
參考。 對於請求文件的所有內容為字節數組:
byte[] contents = fileItem.get();
如果使用的是特立尼達的戰斧 ,則有org.apache.myfaces.trinidad.model.UploadedFile
參考。 為了將文件的所有內容請求為字節數組,可以使用Apache Commons IO的 IOUtils
類:
byte[] contents = IOUtils.toByteArray(uploadedFile.getInputStream());
如果您有org.apache.myfaces.custom.fileupload.UploadedFile
的引用,則更為簡單:
byte[] contents = uploadedFile.getBytes();
更新
如果使用的是Java EE 6,則可以使用Server 3.0規范的新功能來上傳文件,而無需額外的庫。 請參閱BalusC代碼中出色的BalusC教程:在Servlet 3.0中上傳文件
使用休眠http://www.mkyong.com/hibernate/hibernate-save-image-into-database/
要使用JDBC:
要使用Jdbc將圖像寫入數據庫BLOB,您需要將文件轉換為Inputstream。 statement.setBinaryStream(2,(InputStream)inputStream,(int)(image.length())); PreparedStatement語句提供方法setBinaryStream(),該方法用於將二進制流寫入數據庫BLOB列。
以下代碼段可為您提供幫助:
File image = new File("C:/test.jpg");
inputStream = new FileInputStream(image);
Prepared statement = //define as per your table
// set the file binary stream
statement.setBinaryStream(2, (InputStream) inputStream,(int) (image.length()));
statement.executeUpdate();
在您的衣服課中,您可以添加:
@Lob
private byte[] image;
// Getter/Setter methods
得到它的工作! 通過以下代碼:
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
out = response.getWriter();
final String path = "clothesImages" + File.separator + request.getParameter("designer") + File.separator + request.getParameter("ctype") + File.separator + request.getParameter("cname");
out.println(path);
String currentDir = new File(".").getAbsolutePath();
out.println(currentDir);
final Part filePart = request.getPart("image");
final String filename = "image.jpg";
File file = new File(path);
if (!file.exists()){
out.println("Dir Doesn't Exist");
file.mkdirs();
}
OutputStream outstream = null;
InputStream filestream = null;
try{
outstream = new FileOutputStream(new File(path + File.separator + filename));
filestream = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while(( read = filestream.read(bytes)) != -1){
outstream.write(bytes, 0, read);
}
out.println("New file " + filename + " created at " + path);
}catch (FileNotFoundException fne) {
out.println("You either did not specify a file to upload or are "
+ "trying to upload a file to a protected or nonexistent "
+ "location.");
out.println("<br/> ERROR: " + fne.getMessage());
}finally {
if (out != null) {
out.close();
}
if (filestream != null) {
filestream.close();
}
if (outstream != null) {
outstream.close();
}
}
}
我唯一的問題是設置文件路徑。 路徑是
C:\\ Users \\ Chris \\ AppData \\ Roaming \\ NetBeans \\ 7.2.1 \\ config \\ GF3 \\ domain1 \\
但我要保存
Project-war / web / images
有任何想法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.