javaサーブレットライティングフォームCSVへの入力
-
28-10-2019 - |
質問
フォーム入力を解析し、それらから.CSVファイルを作成するサーブレットを構築しようとしていますが、BufferedWriterオブジェクトは、NO(私に明らかな)理由で多くの文字を切り捨てます。
String filepath = getServletContext().getRealPath("\\") + "temp";
String filename = "csv"+dateFormat.format(date)+".csv";
File file = new File(filepath + filename);
file.createNewFile();
BufferedWriter fwrite = new BufferedWriter(new FileWriter(file));
for(int i=0; i<list.size(); i++) {
String[] dataEntry = list.get(i);
for (int j=0; j<dataEntry.length;j++)
fwrite.write("test1-2");
//fwrite.append(dataEntry[j]+";");
fwrite.newLine();
}
fwrite.close();
URI fileUri = file.toURI();
stream = response.getOutputStream();
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment; filename="
+ filename);
URLConnection urlConn = fileUri.toURL().openConnection();
response.setContentLength((int) urlConn.getContentLength());
buf = new BufferedInputStream(urlConn.getInputStream());
while (buf.read() != -1)
stream.write(buf.read());
} finally {
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
コードが少しスラップダッシュである場合は申し訳ありません。各エントリの「test1-2」文字列を書くときの私の現在の出力は
ET-TS12et-TS12et-TS12ÿ
コード自体に関するさらなるコメントは、ネット上で見つけたものを実験しているだけで、実際のベストプラクティスポイントはありません。
解決
個々の方法が過度に大きくないように、いくつかの方法を追加するかもしれません。例えば:
/**
* Saves the List of String[] to the File.
*
* @param f
* @param list
*
* @throws IOException
*/
void saveList(File f, List<String[]> list) throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter(f);
saveList(fw, list);
} finally {
if (null != fw) {
// Ensure that fw is closed.
fw.close();
}
}
}
/**
* Saves the List of String[] to the Writer.
*
* @param w
* @param list
*
* @throws IOException
*/
void saveList(Writer w, List<String[]> list) throws IOException {
BufferedWriter bw = new BufferedWriter(w);
for (int i = 0; i < list.size(); i++) {
String[] dataEntry = list.get(i);
for (int j = 0; j < dataEntry.length; j++) {
bw.write("test1-2");
// bw.append(dataEntry[j]+";");
}
bw.newLine();
}
bw.flush();
}
/**
* Copies in's contents to out.
*
* @param in
* Must not be null.
* @param out
* Must not be null.
*
* @throws IOException
*/
void copyStream(InputStream in, OutputStream out) throws IOException {
if (null == in) {
throw new NullPointerException("in must not be null");
}
if (null == out) {
throw new NullPointerException("out must not be null");
}
byte[] buf = new byte[1024 * 8];
int read = -1;
while ((read = in.read(buf)) > -1) {
out.write(buf, 0, read);
}
}
/**
* Copies in's contents to out, and ensures that in is closed afterwards.
*
* @param in
* Must not be null.
* @param out
* Must not be null.
*
* @throws IOException
*/
void copyStreamAndCloseIn(InputStream in, OutputStream out) throws IOException {
try {
copyStream(in, out);
} finally {
in.close();
}
}
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filepath = getServletContext().getRealPath("\\") + "temp";
String filename = "csv" + dateFormat.format(date) + ".csv";
File file = new File(filepath + filename);
file.createNewFile();
saveList(file, list);
long length = file.length();
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentLength((int) length);
copyStreamAndCloseIn(new FileInputStream(file), response.getOutputStream());
}
奇妙な出力については et-ts12et-ts12et-ts12ÿ
, 、なぜそうなるのかわかりません。
この価値をどのように見ていますか?コンソールに印刷して、その後ファイルを読んでいますか?コンソールに印刷し、別のエディターでファイルを開くことは、使用中の文字エンコードに応じて奇妙な結果を生成する可能性があります。
所属していません StackOverflow