Uploading file to folder and storing the path to Database
All the fellows of java developer who doesnt know how to upload file in web apps, follow these few steps.
first of all download these three files
- commons-fileupload-1.3.1-bin
- commons-fileupload-1.3.1-bin.tar
- commons-io-2.4-bin
then upload this file to libraries in IDE (Netbeans or Eclipse)
then write this code.
// Html Code //
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>upload video</title>
<script>
function myFunction()
{
alert("Are you sure?, dont want to upload your Video??");
window.location.href = "http://www.xyz.com/dashboard.jsp";
}
</script>
</head>
<body>
<form action="insertvideo.jsp" method="post" enctype="multipart/form-data">
<center><strong><h2>Insert Video for company</h2></strong></center><hr>
<label>Video:</label>
<input type="file" class="form-control" name="filev">
<button type="submit">Submit</button>
<button type="button" onclick="myFunction()">Cancel</button>
</form>
</body>
</html>
// JSP CODE //
<%@ page import="java.io.*, javax.servlet.*, java.sql.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@page import="java.util.*, java.math.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String userid=(String) session.getAttribute("user");
if (userid==null) {%>
<jsp:forward page="admin.jsp"/>
<%
}%>
<%
String videoname="art"; //default name for path into DB
File file ;
int maxFileSize = 100000 * 1024; //limit size for file 100MB
int maxMemSize = 100000 * 1024; // 100,000 KB equals 100MB
String filePath = "F:/proj/zot/vid/"; // path of the folder
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>JSP File upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
videoname=fi.getName(); // storing the name of file.
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
Connection con=null;
Statement st=null;
ResultSet rs=null;
String videopath="vid/"+videoname; //now declare variable and set the video path upload into DB.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getconnection("jdbc:odbc:DB_Name");
st=con.createStatement();
String sql="insert into video_table values ('"+videopath+"')";
int x=st.executeUpdate(sql);
if(x!=0)
{%>
<jsp:forward page="dashboard.jsp"/>
<%
}
else{
out.println("Unable to upload");
}
con.close();
st.close();
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}else{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
}
%>
</body>
</html>
No comments:
Post a Comment