This post is about uploading a file using an Adobe Flex application to a Java Servlet using Apache Commons’ FileUpload.
Adobe Flex application
Let’s start with the Adobe Flex application. This application is quite simple and can be found in any tutorial about Adobe Flex’s capability of uploading files. The code below is all one needs.
| <?xml version=”1.0″ encoding=”utf-8″?> <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical”><mx:Script> <
Uploader Flex Application
One now can enter a file title, enter a file description, point to a file to be uploaded and finally upload the file to the servlet.
This was all
Hi!
Thanks for this very intresting tutorial.
In fact, I want to ask you about a problem I’m facing with flex, Servlets and Glassfish V3.
When I execute deploye the servlet, it gets the informations from a form in order to generate a report with Jasperrepots. And this when Glassfish shows an “Error: null” error.
I really don’t know what is causing that problem..:s
Help pleaaaaaaaaaase!!
And thanks in advance.
Hi Nina,
Could you provide me with the stacktrace of the Glassfish log and your servlet’s code?
Regards,
Geert
Hi Geert,
First, thanks for replying. It’s very kind of you.
Here is My servlet code:
public class GetDataServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8″);
PrintWriter out = response.getWriter();
try {
String nom = request.getParameter(“nom”);
String prenom = request.getParameter(“prenom”);
String profession = request.getParameter(“profession”);
String date = request.getParameter(“date”);
String email = request.getParameter(“email”);
String telp = request.getParameter(“telp”);
String telpro = request.getParameter(“telpro”);
System.out.println(nom+” “+prenom+” “+profession+” “+date+” “+email+” “+telp+” “+telpro);
if (nom == null) {
out.println(“false”);
} else {
out.println(“true”);
GenerateReport rep = new GenerateReport();
rep.Generate(nom, prenom, profession, date, telpro, telp);
}
} catch (Exception x) {
System.out.println(“Error: ” + x.getMessage());
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return “Short description”;
}
}
And here is the Generate methode code:
public class CompileReport {
public void Generate() throws JRException{
Map parameters = new HashMap();
parameters.put(“nom”, nom);
parameters.put(“prenom”, prenom);
parameters.put(“profession”, profession);
parameters.put(“date”, date);
parameters.put(“email”, email);
parameters.put(“telp”, telp);
parameters.put(“telpro”, telpro);
JasperFillManager.fillReportToFile(“C:\\Users\\Naoual\\Documents\\NetBeansProjects\\EasyBplan\\rapports\\rapport.jasper”, “C:\\Users\\Naoual\\Documents\\NetBeansProjects\\EasyBplan\\rapports\\rapport.jrprint”,parameters,new net.sf.jasperreports.engine.JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(“C:\\Users\\Naoual\\Documents\\NetBeansProjects\\EasyBplan\\rapports\\rapport.jrprint”, “C:\\Users\\Naoual\\Documents\\NetBeansProjects\\EasyBplan\\rapports\\rapport.pdf”);
} }
Hi Nina,
Shouldn’t this
GenerateReport rep = new GenerateReport();beCompileReport rep = new CompileReport();?And try to use
x.printStacktrace()instead ofSystem.out.println(”Error: ” + x.getMessage());. This will give you more information about what went wrong and also the position in your code where it went wrong.Regards,
Geert
I forgot to tell you that the error disappears when I murge the methode with the servlet. So, I guess the error occurs when the servlet calls the methode.
Concerning the log file, it contains nothing. :s
Thanks again
!!!
Hi Nina,
Are both classes in the same package? If not the servlet class must have an import-statement of the CompileReport class.
Regards,
Geert
Hi Geert,
I use a .jasper (a .jrxml that I have already compiled) that’s why I call directly
GenerateReport rep = new GenerateReport();.The 2 calsses were not at the same package but I’ve added the import statement.
import easybplan.reporting;Regards,
Nina
Does this sample application upload large files? Or is it limited to the post_max_file_size (typically between 3-8MB)?
The post_max_file_size on my server is 8MB… I need to upload files that are between 3MB to 100MB in size… Will this solution work for me?
Hi Josh,
Recently we’ve implemented this solution for a client of ours. Their request was to have an upload limit of 250 Mb. This solution handles this amount of data in chunked message parts, which are received by the HTTP servlet. Remember that this solution only works for 250 Mb when you have set the timeout of the Web Application Server large enough. Also don’ t try to convert the InputStream of the data above a certain amount of Mb’s (in our case 248,5 Mb) to a byte[], because your JVM will throw a Java Heap Out of Memory exception when you do so.
If you use a database like Oracle, try to insert it into a Blob or when using MySQL a LargeBlob.
I haven’t used the post_max_file_size on my Web Application Server. But when it’s possible to alter this value, you might need to do this.
Regards,
Geert