File download with GWT

641 views
Skip to first unread message

Aurita Moya

unread,
Sep 8, 2011, 10:33:59 AM9/8/11
to Google Web Toolkit
I am new to GWT application and I need help downloading files using
servlet, I have the part that generates an excel file. xls, but I need
to display the window "save as"

--------------------------------------------------------------------------------------------------------

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import ve.com.digitel.sorteo.servicios.to.GanadorTo;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Metodo para llamar al servlet que cargara un archivo Excel.
*/
public class FileUpdateExcel extends HttpServlet {
/**
* Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}

/**
* Destroys the servlet.
*/
public void destroy() {
}

/**
* Crear un archivo Excel.
* Realiza la conexion del servlet.
* Metodo que atiende las peticiones que se realizan via Post.
*
* @param request proporcionar la funcionalidad sobre las
peticiones HTTP.
* @param response proporcionar la funcionalidad de respuesta
HTTP.
* @throws ServletException
* @throws java.io.IOException
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse
response)
throws ServletException, java.io.IOException {
//processRequest(request, response);

List<GanadorTo> ganadorTo = new ArrayList<GanadorTo>();
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
for (int i = 0; i < ganadorTo.size(); i++) {
Row row = sheet.createRow(i);
short j = 0, k = 0;
row.createCell(j+
+).setCellValue(ganadorTo.get(i).getId());
sheet.autoSizeColumn(k++);
row.createCell(j+
+).setCellValue(ganadorTo.get(i).getParticipante().getNombre() + " " +
ganadorTo.get(i).getParticipante().getApellido());
sheet.autoSizeColumn(k++);
row.createCell(j+
+).setCellValue(ganadorTo.get(i).getPremio().getNombre());
sheet.autoSizeColumn(k++);
}

SimpleDateFormat formato = new
SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
String cadenaFecha = formato.format(date);

String directorio = "C:\\Documents and Settings\\Aura\\Mis
documentos\\";
File directorioLocal = new File(directorio);//creando
directorio dinamico
if (!directorioLocal.mkdirs())
Window.alert("Error en la Creacion del Archivo de
Respaldo");
else {
String path = directorio + "RespaldoGanadores_" + cadenaFecha
+ ".xls";
File pathFile = new File(path);
FileOutputStream fileOut = new FileOutputStream(pathFile);
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;
filename=" + pathFile);

wb.write(fileOut);
out.flush();
out.close();
fileOut.close();
}
}
}

Can you give me an example to create it through a servlet file
download, please!

Jim Douglas

unread,
Sep 8, 2011, 11:26:21 AM9/8/11
to Google Web Toolkit

Magno Machado

unread,
Sep 8, 2011, 3:44:14 PM9/8/11
to google-we...@googlegroups.com
I use to do this with an Anchor widget that points to the servlet that serve the file

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Magno Machado Paulo
http://blog.magnomachado.com.br
http://code.google.com/p/emballo/

Kevin Jordan

unread,
Sep 8, 2011, 8:26:04 PM9/8/11
to Google Web Toolkit
Yeah, use an anchor which will automatically launch it through the
built-in function in the browser or you can use the open method in the
GWT Window class to open a new window pointing to your servlet:
http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/Window.html#open(java.lang.String,
java.lang.String, java.lang.String)

Window.open is handy if you want to do it automatically without user
interaction or for different components that wouldn't automatically
work through the browser to open it up, like a button.

Jim Douglas

unread,
Sep 8, 2011, 9:06:24 PM9/8/11
to Google Web Toolkit
Window.open() is trapped by the popup blocker.

I posted a link to the complete set of steps.

On Sep 8, 5:26 pm, Kevin Jordan <ke...@kjordan.net> wrote:
> Yeah, use an anchor which will automatically launch it through the
> built-in function in the browser or you can use the open method in the
> GWT Window class to open a new window pointing to your servlet:http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/googl...,
Reply all
Reply to author
Forward
0 new messages