upload file jsp

Solutions on MaxInterview for upload file jsp by the best coders in the world

showing results for - "upload file jsp"
Alice
15 Sep 2017
1<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2    pageEncoding="ISO-8859-1"%>
3    <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
4<%@ page import="javax.servlet.http.*" %>
5<%@ page import="org.apache.commons.fileupload.*" %>
6<%@ page import="org.apache.commons.fileupload.disk.*" %>
7<%@ page import="org.apache.commons.fileupload.servlet.*" %>
8<%@ page import="org.apache.commons.io.output.*" %>
9<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10<html>
11<head>
12<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
13<title>Guru File Upload</title>
14</head>
15<body>
16<%
17   File file ;
18   int maxFileSize = 5000 * 1024;
19   int maxMemSize = 5000 * 1024;
20   String filePath = "E:/guru99/data";
21
22   String contentType = request.getContentType();
23   if ((contentType.indexOf("multipart/form-data") >= 0)) {
24
25      DiskFileItemFactory factory = new DiskFileItemFactory();
26      factory.setSizeThreshold(maxMemSize);
27      factory.setRepository(new File("c:\\temp"));
28      ServletFileUpload upload = new ServletFileUpload(factory);
29      upload.setSizeMax( maxFileSize );
30      try{ 
31         List fileItems = upload.parseRequest(request);
32         Iterator i = fileItems.iterator();
33         out.println("<html>");
34         out.println("<body>");
35         while ( i.hasNext () ) 
36         {
37            FileItem fi = (FileItem)i.next();
38            if ( !fi.isFormField () )  {
39                String fieldName = fi.getFieldName();
40                String fileName = fi.getName();
41                boolean isInMemory = fi.isInMemory();
42                long sizeInBytes = fi.getSize();
43                file = new File( filePath + "yourFileName") ;
44                fi.write( file ) ;
45                out.println("Uploaded Filename: " + filePath + fileName + "<br>");
46            }
47         }
48         out.println("</body>");
49         out.println("</html>");
50      }catch(Exception ex) {
51         System.out.println(ex);
52      }
53   }else{
54      out.println("<html>");
55      out.println("<body>");
56      out.println("<p>No file uploaded</p>"); 
57      out.println("</body>");
58      out.println("</html>");
59   }
60%>
61</body>
62</html>
63
64
Marta
04 Jan 2018
1<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2    pageEncoding="ISO-8859-1"%>
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7<title>Guru File</title>
8</head>
9<body>
10<a>Guru File Upload:</a>
11Select file: <br />
12<form action="action_file_upload.jsp" method="post"
13                        enctype="multipart/form-data">
14<input type="file" name="file" size="50" />
15<br />
16<input type="submit" value="Upload File" />
17</form>
18</body>
19</html>
20