我想检索一个pdf文件,它是存储文件系统,文件名在数据库中。 [英] i wanna retrieve a pdf file that is store file System and the name of the file is in database.

查看:95
本文介绍了我想检索一个pdf文件,它是存储文件系统,文件名在数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AddPhotoServlet.java

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class AddPhotoServlet extends HttpServlet {

    private final String path = "C:/Users/admin/Documents/NetBeansProjects/PDFUploading/PdfFiles";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    String pdf= "";

    if(ServletFileUpload.isMultipartContent(request)){

    try {

          List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
         for(FileItem item : multiparts){
             if(!item.isFormField())
                    {
                        pdf = new File(item.getName()).getName();
                        item.write( new File(path + File.separator + pdf));
                    }
               }


          Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","root");
            con.setAutoCommit(false);

            PreparedStatement ps = con.prepareStatement("insert into pdf values(null,?)");

             ps.setString(1, pdf);
            ps.executeUpdate();

            String sql = "select resume from pdf where resume='"+pdf+"' ";
            ResultSet rs = ps.executeQuery(sql);
            while(rs.next())
            {
                byte[] b1 = rs.getBytes("resume");

                String s1 = new String(b1);
                out.println(s1);
            }


//
//            while(rs.next())
//            {
//                byte[] file= rs.getBytes("resume");
//                String str1 = new String(file);
//                out.println(str1);
//            }
            con.commit();
            con.close();
            out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");

        }catch(Exception ee){
          out.println(ee.getMessage());
          out.println(ee.getCause());
            }
       }
    }
}




index.jsp

&lt;html>
    &lt;head>
        &lt;title>Add Photo&lt;/title>
    &lt;/head>
    &lt;body>
        <h2>Add Photo</h2>
        &lt;form id="form1" enctype="multipart/form-data" action="addphotos" method="post">
            <table>
<!--                <tr>
                    <td>Enter Photo Id :</td>
                    <td>&lt;input  type="text"  name="id"/></td>
                </tr>
                <tr>
                    <td>Enter Title For Photo :</td>
                    <td>&lt;input  type="text"  name="title"/></td>
                </tr>
                <tr>-->
                    <td>Select Photo  </td>
                    <td>&lt;input type="file"  name="photo" />
                </tr>
            </table>
            <p/>
            &lt;input type="submit" value="Add Photo"/>
        &lt;/form>

        <p/>
        <a href="listphotos">List Photos </a>
    &lt;/body>
&lt;/html>

推荐答案

您是否从数据库中获取了PDF?

然后把它扔到你的系统上让系统决定如何处理PDF文件:



So you get the PDF out of your DB?
Then just throw it at your system and let the system decide how to deal with PDF files:

try{ Desktop.getDesktop().open(myFile); }
catch(IOException ex){ /* Logging */ }


这篇关于我想检索一个pdf文件,它是存储文件系统,文件名在数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆