0% found this document useful (0 votes)
28 views

Screen

1) The document contains sample code for an app upload servlet that handles file uploads and saves the files and metadata to directories and a database. 2) It accepts parts from a multipart request containing the app file, logo, and up to 3 images, extracts the file names, writes the files to directories, and inserts a database record. 3) On success, it sets an attribute with a success message and includes a JSP page that displays an alert dialog.

Uploaded by

sanjayshelar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Screen

1) The document contains sample code for an app upload servlet that handles file uploads and saves the files and metadata to directories and a database. 2) It accepts parts from a multipart request containing the app file, logo, and up to 3 images, extracts the file names, writes the files to directories, and inserts a database record. 3) On success, it sets an attribute with a success message and includes a JSP page that displays an alert dialog.

Uploaded by

sanjayshelar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Input Screen

Home

About us
Contact us
Admin Login

Validation
Home – Application
Upload new app
Validation

Upload Logo
Other Images
2 images Upload
3 images Upload
View Request

View User List


View Profile

Update Profile
ANNEXURE 3: SAMPLE CODE

package Admin;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import user.Filename;

import com.ConnectionFactory;

@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*50, // 50MB
maxRequestSize=1024*1024*50)

public class App_upload extends HttpServlet {


private static final long serialVersionUID = -1445651683541116182L;
private static final String SAVE_DIR="Apps";
private static final String SAVE_DIR1="G:\\IEEE\\BE\\Pooja
J\\Fraud\\WebContent\\images";
Connection con=null;
String message = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String savePath = "C:" + File.separator + SAVE_DIR;


String savePath1 = SAVE_DIR1;

ArrayList<String>al=new ArrayList<String>();
al.add(savePath);
al.add(savePath1);
for(String s:al)
{
File fileSaveDir=new File(s);
if(!fileSaveDir.exists()){
fileSaveDir.mkdir();
}
}
String d=request.getParameter("Description");
System.out.println(d);
Part p=request.getPart("Logo");
Part p1=request.getPart("image1");
Part p2=request.getPart("image2");
Part p3=request.getPart("image3");
Part part=request.getPart("app");
long length=part.getSize();
System.out.println(length);

Filename fnn=new Filename();

String fileName=fnn.extractFileName(part);
System.out.println(fileName);
part.write(savePath + File.separator + fileName);
String f1=fnn.extractFileName(p);
p.write(savePath1+"/"+f1);
String f2=fnn.extractFileName(p1);
p1.write(savePath1+File.separator+f2);
String f3=fnn.extractFileName(p2);
p2.write(savePath1+File.separator+f3);
String f4=fnn.extractFileName(p3);
p3.write(savePath1+File.separator+f4);

Date date=new Date();


SimpleDateFormat ft=new SimpleDateFormat("yyyy.MM.dd");

String category=request.getParameter("category");
System.out.println("Category="+category);
con =ConnectionFactory.getInstance().getConnection();
String query="INSERT INTO app (Appname, Description, filepath, Size, Logo,
Image_1, Image_2,Category,Upload_date,Image_3) values (?,?,?,?,?,?,?,?,?,?)";

PreparedStatement pst;
pst=con.prepareStatement(query);
pst.setString(1, fileName);
pst.setString(2, d);
String filePath= savePath + File.separator + fileName ;
pst.setString(3,filePath);
pst.setLong(4, length);

pst.setString(5, f1);

pst.setString(6, f2);

pst.setString(7, f3);
pst.setString(8, category);
pst.setString(9, ft.format(date));
pst.setString(10,f4);
pst.executeUpdate();
message = "File uploaded and saved into database";

request.setAttribute("Message", message);
out.println("<script type=\"text/javascript\">");
out.println("alert('App uploaded successfully');");
out.println("</script>");
getServletContext().getRequestDispatcher("/upload_app.jsp").include(request,
response);
}catch (Exception e) {
e.printStackTrace();
}

}
}

You might also like