public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String userID = (String)session.getAttribute("userID");
//String title = request.getParameter("title");
PrintWriter out = response.getWriter();
out.println("userID is ..."+userID);
String maxfile ="";
String connectionURL = "jdbc:mysql://localhost/cms_college";
Connection connection=null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT maxfile FROM settings");
while(rs.next()){
maxfile = (rs.getString("maxfile"));
out.println("maximum file size allowed is ... "+maxfile);
}
}
catch (Exception e){
}
out.println("<br />Processed,userID=" +userID);
// Check that we have a file upload request
if ( FileUpload.isMultipartContent(request) ) {
// Create a new file upload handler
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
try {
for (Object item : upload.parseRequest(request)) {
processFileItem((FileItem)item);// call method to process each item
}
}
catch (FileUploadException e) {
// Problem when parsing the request data: output an error message.....
System.out.println("Error processing file item: ");
e.printStackTrace(System.out);
}
// Generate some sample HTML
response.setContentType("text/html");
//writeToDB(title);
}
}
public static void writeToDB(String title) {
try{
String connectionURL = "jdbc:mysql://localhost/cms_college";
Connection connection=null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
Statement st = connection.createStatement();
st.executeUpdate ("INSERT INTO cmsarticles (title, userID) VALUES('"+title+"','"+userID+"')"); //userID cannot be resolved.
}
catch(Exception e){
System.out.println("Exception is ;"+e);
}
}
public void processFileItem(FileItem item) {
// Can access all sorts of useful info. using FileItem methods
if (item.isFormField()) {
//Map<String, String> formParameters = new LinkedHashMap<String, String>();
//String filename = item.getFieldName();
//String title = item.getString();
//System.out.println("received filename is ... " + filename );
//formParameters.put(title, filename);
} else {
// Is an uploaded file, so get name & store on local filesystem
String uploadedFileName = new File(item.getName()).getName();
File savedFile = new File("c:/uploads/"+uploadedFileName);
long sizeInBytes = item.getSize();
System.out.println("uploadedFileName is " + uploadedFileName);
String title = uploadedFileName;
System.out.println("size in bytes is " + sizeInBytes);
writeToDB(title);
try {
item.write(savedFile);// write uploaded file to local storage
} catch (Exception e) {
// Problem while writing the file to local storage
}
}
}
}