Java веб-приложение пытается загрузить информацию из API не работает - PullRequest
0 голосов
/ 04 февраля 2020

У меня есть Java веб-приложение, созданное на JSF, которое подключается к API, заполняет данные информацией, и в каждой строке есть кнопка загрузки. Когда я нажимаю кнопку загрузки, он выполняет другой запрос на получение с идентификатором, взятым из строки, по которой была нажата кнопка. Но когда я нажимаю кнопку загрузки, я получаю это сообщение:

emptyResponse: An empty response was received from the server.  Check server error logs.

В журнале сервера нет ошибок. Это код:

package com.serban;

import com.google.common.io.ByteStreams;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Serializable;
import static java.lang.System.out;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipOutputStream;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

@ManagedBean(name = "logic", eager = true)
@SessionScoped
public class Logic implements Serializable {

static JSONObject jsonObject = null;
static JSONObject jo = null;
static JSONArray cat = null;

private ArrayList<Mesaj> logics;
StringBuilder sb = new StringBuilder();
String cif2;
String data_creare2;
String id_solicitare2;
String tip2;
String id2;
String detalii2;
String numar2;

/**
 *
 * @throws ParseException
 */

public void apelareApi()   {

  try {   
 URL url = new URL("xxx");
 URLConnection yc = url.openConnection();
 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
 String inputLine;

 while((inputLine = in.readLine())!= null){
     System.out.println(inputLine);
     sb.append(inputLine+"\n");


 }

 in.close();




 }catch(Exception e) {System.out.println(e);}



}



public void incarcareLista() throws ParseException  {
    JSONParser parser = new JSONParser();

    jsonObject = (JSONObject) parser.parse(sb.toString());
    cat = (JSONArray) jsonObject.get("mesaje");
    logics = new ArrayList<Mesaj>();
        for(int i = 0; i < cat.size(); i++) {
    Mesaj m = new Mesaj();
    jo = (JSONObject) cat.get(i);
    cif2 = jo.get("cif").toString();
    data_creare2 = jo.get("data_creare").toString();
    id_solicitare2 = jo.get("id_solicitare").toString();
    tip2 = jo.get("tip").toString();
    id2 = jo.get("id").toString();
    detalii2 = jo.get("detalii").toString();
    numar2 = Integer.toString(i+1);
    m.setCif(cif2);
    m.setData_creare(data_creare2);
    m.setId_solicitare(id_solicitare2);
    m.setTip(tip2);
    m.setId(id2);
    m.setDetalii(detalii2);
    m.setNumar(numar2);
    logics.add(m);
}


}

/**
 *
 * @throws ParseException
 */
@PostConstruct
public void apelareSiIncarcare()  {
    apelareApi();
    try {
        incarcareLista();
    } catch (ParseException ex) {
        Logger.getLogger(Logic.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public ArrayList<Mesaj> getLogics() {
    return logics;
}

public Logic() {
}

String x;
String id;
byte[] rasp;
public void printNumar(String x) throws IOException  {


    this.x = x;
    //System.out.println(x);


    int y = Integer.parseInt(x);
    jo = (JSONObject) cat.get(y-1);
    System.out.println(jo.get("id"));
    id = jo.get("id").toString();



    rasp = apelareApi2(id);

    startDownload(rasp);



    }
byte[] byteChunk;
public byte[] apelareApi2(String z) throws IOException {


        URL url = new URL("xxx"+z);

        URLConnection yc = url.openConnection();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byteChunk = new byte[is.available()]; // Or whatever size you want to read in at a time.
int n;

while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
   }
   }
  catch (IOException e) {
  System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(),e.getMessage());e.printStackTrace ();}finally {
  if (is != null) { is.close(); }
  }
 return byteChunk;
    }



private void startDownload(byte[] continut)
        throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // externalContext.responseReset();
    externalContext.setResponseHeader("Cache-Control", "public");
    externalContext.setResponseHeader("Pragma", "public");
    externalContext.setResponseHeader("Content-Type", "application/pdf");
    externalContext.setResponseHeader("Content-Length",
            Integer.toString(continut.length));
    externalContext.setResponseHeader("Content-Disposition",
            "attachment;filename=\"" + "id.pdf" + "\"");
    externalContext.getResponseOutputStream().write(continut);
    facesContext.responseComplete();
}


}

1 Ответ

1 голос
/ 04 февраля 2020

Сообщение об ошибке выдается Mojarra JSF, если получен ответ AJAX, который не содержит XML, как указано здесь и здесь в jsf-uncompressed.js:

 * Receive an Ajax response 
 * from the server.
 * Usage:
 * 
 * jsf.ajax.response(request, context);
 * 
 * Implementation Requirements:
 * This function must evaluate the markup returned in the
 * request.responseXML object and perform the following action:
 * 
 * If there is no XML response returned, signal an "emptyResponse"
 * error.

 ...

 } else if (status == "emptyResponse") {
    data.description = "An empty response was received from the server.  Check server error logs.";

Это показывает, что вы используете AJAX. Как заявил Балус C, JavaScript не разрешено запускать диалоговое окно Сохранить документ здесь: Как обеспечить загрузку файла из компонента поддержки JSF?

Таким образом, решение, приближающее вас на один шаг: выключить AJAX.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...