2017-01-31 5 views
0

私は解決策を見回しましたが、解決策は見つけられませんでした。spring URLからファイルを開くタグ

私のローカルドライブにファイルを保存していて、mongoDBにそのファイルのパスを保存しています。 パスを取得した後、DBにパスを渡します。そのURLをクリックすると、関連するファイルを開く必要があります。フロントエンドでそれにアクセスする

"filePath" : "C:/myfiles/Resume_Vipul.docx" 

:私はURLをクリックして

<c:forEach var="ser" items="${services}" varStatus="status"> 
<td class="text-center"><a href='<spring:url value="${ser.filePath}" />'>file</a></td> 
</c:forEach> 

それは私のようにエラーを与えるよう
私のコードはデシベルで

データの通りです

Not allowed to load local resource: file:///C:/myfiles/Resume_Vipul.docx 

この操作を実行する正しい方法は何ですか。 ご協力いただければ幸いです。

+0

'web.xml'であなたのディレクトリにサーブレットマッピングを追加し、' $ {ser.filePath} 'の場所にマッピングエイリアスを使用してください。 –

答えて

0

あなたは

ServletContext cntx= req.getServletContext(); 
    // Get the absolute path of the image 
    String filename = cntx.getRealPath("Images/button.png"); 
    // retrieve mimeType dynamically 
    String mime = cntx.getMimeType(filename); 
    if (mime == null) { 
    resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
    return; 
    } 

    resp.setContentType(mime); 
    File file = new File(filename); 
    resp.setContentLength((int)file.length()); 

    FileInputStream in = new FileInputStream(file); 
    OutputStream out = resp.getOutputStream(); 

    // Copy the contents of the file to the output stream 
    byte[] buf = new byte[1024]; 
    int count = 0; 
    while ((count = in.read(buf)) >= 0) { 
    out.write(buf, 0, count); 
    } 
out.close(); 
in.close(); 

は、クライアント側からのローカルリソースへのアクセス、このような何かをパラメータとしてファイル名を取るコントローラに要求を送信し、応答としてバイトを送り返す必要があります私はないと思いますされ良いアイデア。これをサーバーに別に要求することを検討してください。あなたの場合、Stringパラメータとしてリソースパスをサーバーに送信することができます。

+0

これを実装しようとします –

関連する問題