0

我正在为我的母亲制作一个相当基本的网站,并且看到我在上学期大学期间在 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,我只是不确定文件在传递时应该如何排序以及我应该将什么写入数据库(从我所看到的来看,它是字节 [])

向正确方向伸出援助之手将不胜感激!

4

4 回答 4

1

Once you have the file on the server, either in memory or in a local or temp file (that depends on the framework or libraries that you're using), you will have a reference of a wrapper type.

If you are using Apache Commons File Upload, you have a FileItem reference. For request all contents of the file as byte array:

byte[] contents = fileItem.get();

If you are using Tomahawk of Trinidad, you have a org.apache.myfaces.trinidad.model.UploadedFile reference. For request all contents of the file as byte array, you can use class IOUtils of the Apache Commons IO:

byte[] contents = IOUtils.toByteArray(uploadedFile.getInputStream());

Of if you have a reference of org.apache.myfaces.custom.fileupload.UploadedFile, is more simple:

byte[] contents = uploadedFile.getBytes();

UPDATE

If you are using Java EE 6, you can use new features of Server 3.0 specification for upload files without extra libraries. See the excellent tutorial of BalusC in The BalusC Code: Uploading files in Servlet 3.0

于 2013-05-20T17:51:34.887 回答
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();
于 2013-05-20T17:15:09.353 回答
0

得到它的工作!有点,用这段代码:

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\

但我希望它保存在

项目战争/网络/图像

有任何想法吗?

于 2013-05-21T16:41:29.733 回答
0

在您的 Clothes 类中,您可以添加:

@Lob
private byte[] image;

// Getter/Setter methods
于 2013-05-20T17:46:21.390 回答