JSPることができるzip corruptsのファイル
質問
ん問題になってzipファイルにJSP.
Zipファイルは、常に腐敗に終わった後はデータのダウンロードまた、複数の方法で読み込みおよび書き込みにはなってないですよね。
フィギュアで追加ascii文字のどこかのファイルを開くには、表示すべてのファイル名だけを抽出せます。
ここでの私の最新コード:
<%@ page import= "java.io.*" %>
<%
BufferedReader bufferedReader = null;
String zipLocation = "C:\\zipfile.zip";
try
{
bufferedReader = new BufferedReader(new FileReader(zipLocation));
response.setContentType("application/zip");
response.setHeader( "Content-Disposition", "attachment; filename=zipfile.zip" );
int anInt = 0;
while((anInt = bufferedReader.read()) != -1)
{
out.write(anInt);
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
編集:に引っ越しのコードをサーブレットだけではなかった。しろありっことは、この最新の非コード:
public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
{
try
{
String templateLocation = Config.getInstance().getString("Site.templateDirectory");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=output.zip;");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
FileInputStream fis = new FileInputStream(templateLocation);
int len;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) > 0)
{
bos.write(buf, 0, len);
}
bos.close();
PrintWriter pr = response.getWriter();
pr.write(baos.toString());
pr.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
EDIT2:
これはサーブレットコードするんです。記念tシャツ完売のお知らせ
public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
{
try
{
String templateLocation = Config.getInstance().getString("Site.templateDirectory");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=output.zip;");
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
FileInputStream fis = new FileInputStream(templateLocation);
int len;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) > 0)
{
bos.write(buf, 0, len);
}
bos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
解決
Zipsはバイナリファイル、行ってはならないのに適しているとして文字データです。またテキスト以外のコードが破損のファイルです。
使用プレーンガーデンプレイスの片隅、サーブレットの代わりにJSP.
他のヒント
JSPの追加空白文字を出力します。をご提案いたしますservlet.
また、お客様のみ ストリップの空白文字からjsp出力 なくなったことの影響のないZIP出力そのものです。
問題はこの正確なスニペットがオンラインについて白線以外JSPタグを送付するためのブラウザでHTMLとして(空行でHTMLソース).
うよう十分注意してください削除をなんてできない <%%>は、特に空白いの端末の改行!), またはアドバイスに従ってみましょう、他の地域では行うことができな用servlet:)
おservletコードのない出力ストリームへ response.getWriter()
, として指摘で 文字データ.し見積もりのJavadoc:
PrintWriter getWriter() throws IOException
を返しまPrintWriterオブジェクトに発信できる文字テキストするサービスです。のPrintWriterの文字エンコーディングで返されるgetCharacterEncoding()
なんでも書面でのバイト配列の Writer
, きの結果 ByteArrayOutputStream.toString()
, または文字の変換~
利用したいです。getOutputStream()のようなもの:
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
そのバイトの内容のファイルです。
読者は、通常のための文字ストリームが私の記憶が正しければ挑戦しますのでByteArrayOutputStream?また、JSPもしれないというcorruptingに出力しています。"
bufferedReader = new BufferedReader(new FileReader(zipLocation));
ほかのServlet/JSP、この行をcorruptingデータを100%確実になった。することを試みま解釈のバイナリデータとしてテキストのデフォルトエンコードは、その約半分をバイトのファイルが"未知の文字から"と言いました。
バイト文字列は同じものではありません!
またこの利用は平野およびサーブレットを考える ロバート-Munteanuの回答 しながらも、なにができることをJSPます。の問題なの空白文字がこの潜在変数"out"のインスタンスのJSPWriter、ライターはキャラクターが、バイトです。利用するようにして に応じます。getOutputStream() 右できます。また例外との人たはOutputStreamに書き込みができる無視されます。
したように、サーブレットにも非常に清浄機。