Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的下载Servlet运行了两次_Java_Gwt_Servlets_Download - Fatal编程技术网

Java 我的下载Servlet运行了两次

Java 我的下载Servlet运行了两次,java,gwt,servlets,download,Java,Gwt,Servlets,Download,我有一个带有下载servlet的gwt web应用程序,我统计数据库中每个文件的下载量。当用户向服务器发送下载请求时,我的下载servlet运行两次,每次我的下载计数器计数为2。但用户发送请求的文件已下载一次。我不知道我的servlet对一个请求运行2次是什么 我的下载servlet: public class DownloadServlet extends HttpServlet { private static final String UPLOAD_DIRECTORY = "c:\\u

我有一个带有下载servlet的gwt web应用程序,我统计数据库中每个文件的下载量。当用户向服务器发送下载请求时,我的下载servlet运行两次,每次我的下载计数器计数为2。但用户发送请求的文件已下载一次。我不知道我的servlet对一个请求运行2次是什么

我的下载servlet:

public class DownloadServlet extends HttpServlet {

 private static final String UPLOAD_DIRECTORY = "c:\\update\\";
 private Connection con;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {

     Statement select=null;
     ResultSet result=null;

     try{

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/laplasdb?useUnicode=true&characterEncoding=UTF-8","gwt","root");
        }catch(Exception ex){

        }
     String query = "SELECT * FROM TBL_Drive_Files WHERE id = '"+req.getParameter("fileid")+"'", filename="";
     InputStream filecontent=null;

     int filesize=0;
     try {
         select = con.createStatement();
         result = select.executeQuery(query);
         while (result.next()) {
             filename = result.getString(2);
             filesize = result.getInt(4);
             filecontent = result.getBinaryStream(5);
         }

         result.close();

     } catch(SQLException e) {
     }


     BufferedOutputStream output = null; 
     try { 

         resp.reset(); 
         resp.setContentType("application/octet-stream"); 
         resp.setContentLength(filesize); 
         resp.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\""); 
         output = new BufferedOutputStream(resp.getOutputStream()); 
         for(int data; (data=filecontent.read()) != -1;) { 
             output.write(data); 
         } 
         output.flush(); 

         query = "Update TBL_Drive_Files SET count = count + 1 WHERE id = '"+req.getParameter("fileid")+"'";
         try {
            select = con.createStatement();
            select.execute(query);              

            select.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


     } 
     catch (IOException e) { 

         e.printStackTrace(); 
     } 
     finally {
     }
 }     
}

在客户端:

Button bd = new Button("Download", new ClickHandler(){
             public void onClick(ClickEvent event){
                final String link = GWT.getModuleBaseURL() + "download?fileid=3";

                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,link);

                try {
                     builder.sendRequest(null, new RequestCallback() {
                     public void onError(Request request, Throwable t) {
                        Window.alert("Error bei getExcel");
                    }
                    public void onResponseReceived(Request request,Response response){
                            int statuscode = response.getStatusCode();
                            if(statuscode == 200) {
                                    Window.Location.replace(link);
                      } else if(statuscode == 404) {
                        Window.alert("Service not available.");
                      }
                    }
                   });
                   } catch (RequestException re) {
                       Window.alert(re.toString());
                   }
             }
    });             
    RootPanel.get().add(bd);

你提出两个要求。一个是调用
builder.sendRequest(null,newrequestcallback()
另一个在回调中,状态为200
Window.Location.replace(link)

顺便说一句,我希望这不是一个代码,将部署

  • 未处理的异常
  • 数据库操作应该在自己的层中完成
  • 我看不到任何代码约定
  • 您应该只选择您真正需要的列
  • 不必要的声明
还有许多其他要点,但您应该首先解决这个问题,然后您将能够发现错误并自己维护代码

摘自我的答案:

一般(*)您不能使用ajax下载文件,因此必须使用Window.open()或iframe要求用户将文件另存为。

因此,不要使用
RequestBuilder
来获取文件,正如我在:


最好的解决方案是在iframe中打开文件,但您必须在服务器端设置适当的内容处置头,这会导致浏览器显示“保存”对话框。

超出主题。关闭funally block中的资源,在第一次查询中使用PreparedStatement。使用特定的方法更新
executeUpdate
。此外,我建议您阅读代码约定。也许一个好的旧的逐行运行可以帮助您找到计数器增加2的原因。检查SQL查询是否运行了2次,或者是否包含rements by 2,这可能会对您有所帮助我逐行编译代码,servlet运行了2次。但我找不到运行servlet的命令实际上我认为servlet被调用了两次以响应2个不同的请求,让我们查找它们。我将请求代码放在上面。是否有重复的请求?当我删除Window.Location.replace(link)时根据我的代码,servlet只运行一次,但下载操作无法正常工作。那么我能做什么呢?我不知道您的要求,但如果足够的话,您可以摆脱requestBuilder,只使用
Window.location.replace(…)
查看手册(根据您的错误消息,我认为德语链接可以)