FileDocCategorySizeDatePackage
UploadServlet.javaAPI DocGlassfish v2 API6899Fri May 04 22:34:08 BST 2007com.sun.enterprise.admin.server.core.servlet

UploadServlet

public class UploadServlet extends HttpServlet
This servlet is used to upload files to application server tmp directory

Fields Summary
private static final Logger
sLogger
Constructors Summary
Methods Summary
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


         
           

        // check if fileName is valid
        String str = request.getQueryString();
        Properties p = new Properties();
        try {
            getQueryProperties(str, p);
        } catch (Exception e) {
            writeErrorResponse(response, e.getMessage());
            return;
        }
        String fileName = p.getProperty("file");

        if (fileName == null || fileName.trim().equals("")) {
            writeErrorResponse(response, "no fileName found");
            return;
        }

        // use tmp directory specified in domain.xml
        File localDir = new File(AdminService.getAdminService().getTempDirPath());
        localDir.mkdirs();

        // check pre-existing file
        File uploadFile = new File(localDir, fileName);
        if (uploadFile.exists()) {
            sLogger.log(Level.INFO, "mbean.temp_upload_file_exists", uploadFile.getCanonicalPath());
            if (!uploadFile.delete()) {
                sLogger.log(Level.INFO, "mbean.delete_temp_file_failed", uploadFile.getCanonicalPath());
                writeErrorResponse(response, "cannot delete existing file");
                return;
            }
            sLogger.log(Level.FINE, "mbean.delete_temp_file_ok", uploadFile.getCanonicalPath());
        }   
        
        sLogger.log(Level.INFO, "mbean.begin_upload", uploadFile.getCanonicalPath());
        
            
        // read data from inputstream
        FileOutputStream fos = null;
        InputStream is = null;
        
        try {
            fos = new FileOutputStream(uploadFile);
            is = request.getInputStream();
            byte[] buffer = new byte[1024*64];
            
            for (int i = is.read(buffer); i > 0; i = is.read(buffer)) {
                fos.write(buffer, 0, i);
            }
        } catch (Exception e) {
            sLogger.log(Level.WARNING, "mbean.upload_failed", uploadFile.getCanonicalPath());
            writeErrorResponse(response, "uploading file failed");
            return;
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (is != null) {
                is.close();
            }
        }

        // write remoteFilePath to HTTP response
        writeResponse(response, uploadFile.getCanonicalPath());
        return;

    
public voidgetQueryProperties(java.lang.String q, java.util.Properties toAddTo)

        if (q == null || q.length() == 0) {
            return;
        }
        for (StringTokenizer iter = new StringTokenizer(q, "&");
            iter.hasMoreElements();/*-*/) {
            String pair = (String) iter.nextToken();
            int split = pair.indexOf('=");
            if (split <= 0) {
                throw new Exception ("Invalid pair [" + pair
                    + "] in query string [" + q + "]");
            } else {
                String key = pair.substring(0, split);
                String value = pair.substring(split + 1);
                try {
                    key = URLDecoder.decode(key, "UTF-8");
                    value = URLDecoder.decode(value, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new Exception("Invalid encoding in [" + pair
                        + "] in query string [" + q + "]" + e.getMessage());
                }
                toAddTo.setProperty(key, value);
            }
        }
    
private voidwriteErrorResponse(javax.servlet.http.HttpServletResponse response, java.lang.String errorMsg)

        response.setContentType("text/html");
        response.setBufferSize(8192);
        PrintWriter writer = response.getWriter();
        writer.println("FAIL:" + errorMsg);
        writer.close();
    
private voidwriteResponse(javax.servlet.http.HttpServletResponse response, java.lang.String msg)

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.setBufferSize(8192);
        PrintWriter writer = response.getWriter();
        writer.println("SUCCESS:" + msg);
        writer.close();