Permite setear desde Flex el nombre de fichero y la ruta de salvado, en los parámetros Filename y Remotefolder respectivamente.
Posteo el fuente completo , solo habéis de crear ambos ficheros en el mismo paquete, y mapear el servlet en web.xml, luego en Flex, habréis de indicar como url de destino a Filereference la dirección mapeada del servlet ("/upload" por ejemplo) y listo.
Espero que os sirva
UploadServlet.java
Código :
package flex.fileupload; /* * UploadServlet.java * * Created on 22 de November de 2007, 3:31 * */ /** * * @author Pepe Barragan (MorphX) * @company Soft Evolutions Technology */ import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; import java.io.File; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; public class UploadServlet extends HttpServlet { Hashtable<Object, Vector> parameters = new Hashtable<Object, Vector>(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); try { ServletContext context = (ServletContext) this.getServletContext(); String realPath = context.getRealPath("/"); out.print(realPath + "<- Success :)"); out.close(); } catch (Exception ex) { out.print("FAILED :(" + ex.getMessage()); out.close(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); try { ServletContext context = (ServletContext) this.getServletContext(); String realPath = context.getRealPath("/"); //10Mb filesize allow. saveUpload(req, realPath, (1024 * 1024 * 10)); out.print("SUCCESS :)"); out.close(); } catch (Exception ex) { out.print("FAILED :(" + ex.getMessage()); out.close(); } } //Adapted by MorphX from com.oreilly.servlet multipart library public void saveUpload(HttpServletRequest request, String saveDirectory, int maxPostSize) throws IOException { // Sanity check values if (request == null) { throw new IllegalArgumentException("request cannot be null"); } if (saveDirectory == null) { throw new IllegalArgumentException("saveDirectory cannot be null"); } if (maxPostSize <= 0) { throw new IllegalArgumentException("maxPostSize must be positive"); } // Parse the incoming multipart, storing files in the dir provided, // and populate the meta objects which describe what we found MultipartParser parser = new MultipartParser(request, maxPostSize, true, true); // Some people like to fetch query string parameters from // MultipartRequest, so here we make that possible. Thanks to // Ben Johnson, [email protected], for the idea. if (request.getQueryString() != null) { // Let HttpUtils create a name->String[] structure Hashtable queryParameters = HttpUtils.parseQueryString(request.getQueryString()); // For our own use, name it a name->Vector structure Enumeration queryParameterNames = queryParameters.keys(); while (queryParameterNames.hasMoreElements()) { Object paramName = queryParameterNames.nextElement(); String[] values = (String[]) queryParameters.get(paramName); Vector newValues = new Vector(); for (int i = 0; i < values.length; i++) { newValues.add(values[i]); } parameters.put(paramName, newValues); } } Part part; while ((part = parser.readNextPart()) != null) { String name = part.getName(); if (part.isParam()) { // It's a parameter part, add it to the vector of values ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); Vector existingValues = (Vector) parameters.get(name); if (existingValues == null) { existingValues = new Vector(); parameters.put(name, existingValues); } existingValues.addElement(value); } else if (part.isFile()) { String savepath = saveDirectory + getParameter("Remotefolder"); File dir = new File(savepath); // Check saveDirectory is truly a directory if (!dir.isDirectory()) { throw new IllegalArgumentException("Not a directory: " + savepath); } // Check saveDirectory is writable if (!dir.canWrite()) { throw new IllegalArgumentException("Not writable: " + savepath); } // It's a file part FilePart filePart = (FilePart) part; FileRenamer newName = new FileRenamer(); newName.setFilename(getParameter("Filename")); filePart.setRenamePolicy(newName); String fileName = getParameter("Filename"); if (fileName != null) { filePart.writeTo(dir); } } } } public String getParameter(String name) { try { Vector values = (Vector) parameters.get(name); if (values == null || values.size() == 0) { return null; } String value = (String) values.elementAt(values.size() - 1); return value; } catch (Exception e) { return null; } } }
FileRenamer.java
Código :
package flex.fileupload; import com.oreilly.servlet.multipart.FileRenamePolicy; import java.io.*; public class FileRenamer implements FileRenamePolicy { private String filename = "unnamed.dat"; // This method does not need to be synchronized because createNewFile() // is atomic and used here to mark when a file name is chosen public File rename(File f) { f = new File(f.getParent(), filename); return f; } public void setFilename(String fn) { filename = fn; } }