使用Java打开一些文档

时间:2019-09-06 06:20:32

标签: java solr

我有一个index.jsp页面。在此页面中,我将用一些获得Solr数据库的记录动态填充表。我的脚本是这样的;

<button onclick="IncreaseFontSize()">+</button>
<button onclick="DecreaseFontSize()">-</button>

<div class="font-varies">
  Hello
</div>

<div class="font-constant">
  how are you?
</div>

在此代码段中,我正在打开文档(pdf,docx ...)

<script>
if($.fn.dataTable.isDataTable( '#example' )){
    var table = $('#example').DataTable();
}
function getData(){

    $('#example tbody').html('');
    var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO_copy:";
    var URL_MIDDLE="AND PackName_copy:";
    var URL_SUFFIX="AND DocType_copy:";
    var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
    var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
    var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
    var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;
    $.ajax({
        url:URL,
        dataType:'jsonp',
        jsonp : 'json.wrf',
        type :'get',
        cache :false,
        success: function(data){
            var docs=data.response.docs;
            var html='';        
            $.each(docs,function(key,value){
                var arrayExtensions=["jpg","JPG","JPG File","jpeg","JPEG image","PNG","TIFF image","tiff"];
                html+='<tr>';
                html+='<td>'+value.id+'</td>';
                html+='<td>'+value.strSO+'</td>';
                html+='<td class="text-center">'+value.PackName+'</td>';
                html+='<td class="text-center">'+value.DocType+'</td>';
                html+='<td class="text-center">'+value.DocName+'</td>';
                html+='<td class="text-center"><form method="POST" action="openDocumentServlet" target="_blank"><input name="document" value="'+value.FilePath+'" hidden><input name="docName" value="'+value.FileName+'" hidden><input id="buton" type="submit"  class="btn btn-sm" value="OPEN"></form></td>';
                html+='<td class="text-center" onmouseover="mouseOver(this)" onmouseout="mouseOut(this)"><p><a href=""><img src="images//SoftwareIcons-21-512.png" width=50; height=50; name="'+value.FileName+'" id="'+value.FilePath+'" alt="'+arrayExtensions.indexOf(value.extType[0])+'"></a></p></td>';
                html+='</tr>';
            });

            $('#example').DataTable().destroy();
            $('#example tbody').html(html);
            var table=$('#example').DataTable({
                "aaSorting" : [],

            });
        },
    });


};
</script>

我正在使用servlet来做到这一点。我的servlet代码是这样的。

html+='<td class="text-center"><form method="POST" action="openDocumentServlet" target="_blank"><input name="document" value="'+value.FilePath+'" hidden><input name="docName" value="'+value.FileName+'" hidden><input id="buton" type="submit"  class="btn btn-sm" value="OPEN"></form></td>';

我的文档在服务器端。我正在复制它的本地路径,例如protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String Docpath=request.getParameter("document"); String docname=request.getParameter("docName"); File file=new File("T:/Temp/"+docname); Path filePath=Paths.get("T:/Temp/"+docname); response.setContentType(Files.probeContentType(filePath)); response.setHeader("Content-Disposition", "inline;filename="+docname); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { ServletOutputStream outs = response.getOutputStream (); File original=new File(Docpath); File destination=new File("T:/Temp/"); FileUtils.copyFileToDirectory(original, destination); InputStream input=new FileInputStream(file); bis=new BufferedInputStream(input); bos=new BufferedOutputStream(outs); byte[] buf = new byte[2048]; int bytesRead; while ((bytesRead = bis.read(buf)) > 0) { bos.write(buf, 0, bytesRead); } }catch(IOException e){ e.printStackTrace(); }finally { bis.close(); bos.close(); } doGet(request, response); } ,然后在本地路径中打开文档。有时它之前正确运行,但现在却给了我这个错误。

T:/Temp/

0 个答案:

没有答案