我已经尝试了几个地方插入finally块,但是无论我尝试什么,最终都会使代码变得更糟。

这是我的代码,最后一个大括号的第四个是给我错误的代码。有什么想法吗?


    package com.tunestore.action;

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.PreparedStatement;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;


    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import org.apache.struts.action.DynaActionForm;

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.owasp.validator.html.*;
    import org.owasp.esapi.*;

    public class DownloadAction extends Action
    {
      private static final Log log = LogFactory.getLog(DownloadAction.class);

      public static String DB_URL;

        static
        {
                if (System.getProperty("tunestore.db.location") != null)
                {
                    DB_URL = "jdbc:derby://localhost:1527/" + System.getProperty("tunestore.db.location");
                }
                else
                {
                    DB_URL = "jdbc:derby://localhost:1527/" + System.getProperty("user.home") + "/.tunestore";
                }
                System.setProperty("jdbc.tunestore.url", DB_URL);
        }

        public static Connection getConnection() throws Exception
        {
            log.info("Opening database at " + DB_URL);
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            Connection conn = DriverManager.getConnection(DB_URL);
            return conn;
        }


        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
        {
            DynaActionForm daf = (DynaActionForm)form;
            String user = (String)request.getSession(true).getAttribute("USERNAME");
            if(user != null)
            {
                Connection conn = null;
                try
                {
                    conn = DownloadAction.getConnection();
                    String sql2 = "SELECT ID FROM CD WHERE CD.BITS = ?";
                    PreparedStatement stmt2 = conn.prepareStatement(sql2);
                    stmt2.setString(1, request.getParameter("cd"));
                    ResultSet rs2 = stmt2.executeQuery();
                    rs2.next();

                    String sql = "SELECT COUNT(*) "
                    + "FROM TUNEUSER_CD "
                    + "WHERE TUNEUSER_CD.TUNEUSER = ? AND TUNEUSER_CD.CD = ?";
                    PreparedStatement stmt = conn.prepareStatement(sql);
                    stmt.setString(1, user);
                    stmt.setInt(2, rs2.getInt(1));
                    ResultSet rs = stmt.executeQuery();
                    rs.next();
                    int owned = rs.getInt(1);

                    if(owned == 1)
                    {
                        try
                        {
                            // Try to open the stream first - if there's a goof, it'll be here
                            InputStream is = this.getServlet().getServletContext().getResourceAsStream("/WEB-INF/bits/" + request.getParameter("cd"));

                            if (is != null)
                            {
                                response.setContentType("audio/mpeg");
                                response.setHeader("Content-disposition", "attachment; filename=" + daf.getString("cd"));
                                byte[] buff = new byte[4096];
                                int bread = 0;
                                while ((bread = is.read(buff)) >= 0)
                                {
                                    response.getOutputStream().write(buff, 0, bread);
                                }
                            }
                            else
                            {
                                ActionMessages errors = getErrors(request);
                                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("download.error"));
                                saveErrors(request, errors);
                                return mapping.findForward("error");
                            }
                        }

                        catch (Exception e)
                        {
                            e.printStackTrace();
                            ActionMessages errors = getErrors(request);
                            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("download.error"));
                            saveErrors(request, errors);
                            return mapping.findForward("error");
                        }

                        return null;

                    }
                }
            }
        }
    }

最佳答案

该括号是您的外部try块结束的位置。它没有catch块,也没有finally块,因此会出现错误。只需在括号后添加一个或另一个即可,如果不需要,请删除try

08-18 16:05