Buongiorno a tutti, stavo sistemando questo problema sul service che deve stampare la listamoto sul file excel e poi scaricarlo tramite il metodo richiamato dall'endpoint /exportMotoToExcel. Se non inserisco il path assoluto del file mi da questo errore : java.io.FileNotFoundException: WriteMotoToExcel.xls (Accesso negato)
In sostanza questo metodo funziona solamente se gli passo un path assoluto e non funziona con un path relativo del tipo WriteToExcelFile.writeMotoListToFile("WriteMotoToExcel.xls", listaMoto);
@GET
@Path("/exportMotoToExcel")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportToExcel(@QueryParam("model") String model, @QueryParam("ccFrom") Integer ccFrom,
@QueryParam("ccTo") Integer ccTo, @QueryParam("yearProdFrom") String yearProdFrom,
@QueryParam("yearProdTo") String yearProdTo, @QueryParam("priceFrom") Integer priceFrom,
@QueryParam("priceTo") Integer priceTo, @QueryParam("orderBy") @DefaultValue("anno") String orderBy,
@QueryParam("orderHow") @DefaultValue("desc") String orderHow, @QueryParam("offset") Integer offset,
@QueryParam("limit") Integer limit) {
SqlSessionFactory sessionFactory = SqlSessionFactoryManager.getSqlSessionFactory();
SqlSession session = sessionFactory.openSession();
List<Moto> listaMoto = null;
RowBounds rowBounds = createRowBounds(offset, limit);
MotoExample mb = new MotoExample();
MotoExample.Criteria criteria = createCriteria(mb, model, ccFrom, ccTo, yearProdFrom, yearProdTo, priceFrom,
priceTo);
if (orderBy != null && orderHow != null) {
mb.setOrderByClause(orderBy + " " + orderHow + ", modello " + "asc");
}
try {
MotoMapper motoMapper = session.getMapper(MotoMapper.class);
listaMoto = motoMapper.selectByExampleWithRowbounds(mb, rowBounds);
// OCCHIO A PASSARE TUTTO IL FILE PATH ALTRIMENTI NON FUNZIONA!!!!
WriteToExcelFile.writeMotoListToFile(
"C:\\Users\\ricca\\eclipse-workspace\\RestApiMyBatis\\WriteMotoToExcel.xls", listaMoto);
File file = new File("C:\\Users\\ricca\\eclipse-workspace\\RestApiMyBatis\\WriteMotoToExcel.xls");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename=WriteMotoToExcel.xls");
return response.build();
} catch (Exception e) {
logger.error(e.getMessage(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("internal server error").build();
} finally {
session.close();
}
}