gpt4 book ai didi

java - 如何编写 CSV 文件并在浏览器中打开它

转载 作者:行者123 更新时间:2023-12-01 13:23:28 26 4
gpt4 key购买 nike

我正在尝试在新浏览器中打开文件,而不是将其保存在本地。

下面是我的 servlet,它保存到我的本地驱动器中,但我想在浏览器中打开它。我认为我应该使用 OutPutStream 而不是 FileWriter,但我了解如何使用它。

  protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
ResultSet result = null;
PreparedStatement selectStmt = null;
try {
conn = DBUtil.getConnection();
if(request.getParameter("param1").equals("RED9")){
selectStmt = conn.prepareStatement("select * from TableName");
}else if(request.getParameter("param1").equals("RED921")){
selectStmt = conn.prepareStatement("select * from TableName");
result = selectStmt.executeQuery();
FileWriter f = new FileWriter("c://myFile.cvs);
while(result.next()){

writer.append(result.getString("PARTC_SSN"));
writer.append(',');

writer.append("\n");
}
writer.flush();
writer.close();
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if (result != null) {
result.close();
result = null;
}
if (selectStmt != null) {
selectStmt.close();
selectStmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}

} catch (SQLException e) {
DBUtil
.log("Exception from ExcelReportServlet class, Method -createNoEcmatsRegDoc "
+ e);
}
}

}

最佳答案

有两件事。首先,将文件编写器替换为响应编写器,并确保告诉响应期望正确的输出格式。另外,如果发生异常,请务必关闭编写器。不管怎样,这是我对你的代码应该是什么样子的猜测:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gov.ca.water.preciptemp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author hdunsford
*/
public class DailyHydroServlet1 extends HttpServlet {

private static final Logger logger = Logger.getLogger(DailyHydroServlet1.class.getName());

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html;charset=UTF-8");
Connection conn = null;
ResultSet result = null;
PreparedStatement selectStmt = null;
PrintWriter out;
try {
out = response.getWriter();
conn = DBUtil.getConnection();
if (request.getParameter("param1").equals("RED9")) {
selectStmt = conn.prepareStatement("select * from TableName");
} else if (request.getParameter("param1").equals("RED921")) {
selectStmt = conn.prepareStatement("select * from TableName");
result = selectStmt.executeQuery();

while (result.next()) {

out.append(result.getString("PARTC_SSN"));
out.append(',');

out.append("\n");
}
out.flush();
out.close();
}
} catch (Exception ex) {
try{
out.close();
}catch(Exception e){
//ignore
}
logger.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (result != null) {
result.close();
result = null;
}
if (selectStmt != null) {
selectStmt.close();
selectStmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}

} catch (SQLException e) {
DBUtil.log("Exception from ExcelReportServlet class, Method -createNoEcmatsRegDoc "
+ e);
}
}

}


// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

关于java - 如何编写 CSV 文件并在浏览器中打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21912780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com