All pastes #181536 Raw Edit

Something

public java v1 · immutable
#181536 ·published 2006-09-25 01:54 UTC
rendered paste body
package com.robinson;import com.robinson.file.SendFile;import com.robinson.sql.DbData;import com.oreilly.servlet.MultipartRequest;import com.oreilly.servlet.multipart.MultipartParser;import com.oreilly.servlet.multipart.Part;import com.oreilly.servlet.multipart.ParamPart;import com.oreilly.servlet.multipart.FilePart;import com.sun.java_cup.internal.parser;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.ServletException;import javax.servlet.ServletConfig;import java.io.*;import java.sql.Connection;import java.sql.DriverManager;/** * @author Sean Robinson * @version v. */public class FileServlet extends HttpServlet {    private String dbtype;    private String dburl;    private String filepath;    private String pagetitle;    private String headertitle;    private Connection dbcon;    public String path(HttpServletRequest request, String path) throws ServletException {        return request.getContextPath() + request.getServletPath() + "/" + path;    }    public void init(ServletConfig config) throws ServletException {        super.init(config);        this.dbtype = config.getInitParameter("dbtype");        this.dburl = config.getInitParameter("dburl");        this.pagetitle = config.getInitParameter("pagetitle");        this.headertitle = config.getInitParameter("headertitle");        try {            Class.forName(dbtype);            dbcon = DriverManager.getConnection(dburl,"","");            DbData db = new DbData();            this.filepath = db.getFilePath(dbcon);        } catch(Exception e){            throw new ServletException("Database Connection Error on init",e);        }    }    public void printHome(HttpServletRequest request, HttpServletResponse response) throws ServletException{        response.setContentType("text/html");        try {        PrintWriter out = response.getWriter();        out.println("<form action="+ path(request,"uploadnow")+ "method=\"post\" enctype=\"multipart/form-data\">\n" +                "  <p>\n" +                "    <input type=\"file\" name=\"file\" />\n" +                "</p>\n" +                "  <p>\n" +                "    <input type=\"submit\" name=\"Submit\" value=\"Submit\" />\n" +                "</p>\n" +                "</form>");    } catch(Exception e){            throw new ServletException("error",e);        }    }    public void uploader(HttpServletRequest request,HttpServletResponse response) throws ServletException {        response.setContentType("text/html");        int i;        try {            i = 4096;            String fname = null;            String lname= null;            String email = null;            String filename = null;            MultipartParser mp = new MultipartParser(request,i);            Part part;            while((part = mp.readNextPart())!=null){            if((part = mp.readNextPart()) == null){                //send them back to upload page            } else if (part.isFile()){                FilePart fp = (FilePart) part;                filename = fp.getFileName();                File fileup = new File(filepath+filename);                fp.writeTo(fileup);           } else if(part.isParam()){                if(part.getName().equals("fname")){                    ParamPart param;                    fname=param.getStringValue();                }            }        }        }catch(Exception e){    }    }    public void handle(HttpServletRequest request,HttpServletResponse response) throws ServletException {        String path = request.getPathInfo();        try{        if(path !=null){            if(path.equals("/Home")){                printHome(request,response);            } else if(path.equals("/uploadnow")){                uploader(request,response);            }        } else {            response.sendRedirect(path(request,"/"));        }    } catch(Exception e){            throw new ServletException("handle error",e);        }    }    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException {    }    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {        doGet(request,response);    }}