So, first of all add common-fileupload.1.1.1.jar or
Struts1.3.8-common-fileupload.1.1.1.jar (with Struts)
No problem if you are Netbean user then simply right click on
libraries-> add library->select Struts
Example:
Multiple file Upload
|
|
Specify
file:
|
|
Specify
file:
|
|
Specify
file:
|
|
Index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title></title>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<form
action="upload_file_multiple.jsp" method="post"
enctype="multipart/form-data" name="form1"
id="form1">
<center>
<table
border="2">
<tr>
<td
align="center"><b>Multiple file Upload</b></td>
</tr>
<tr>
<td>
Specify file: <input
name="file" type="file" id="file">
<td>
</tr>
<tr>
<td>
Specify file:<input
name="file" type="file" id="file">
</td>
<tr>
<td>
Specify file:<input
name="file" type="file" id="file">
</td>
</tr>
<tr>
<td align="center">
<input
type="submit" name="Submit" value="Submit
files"/>
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
upload_file_multiple.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%@ page import="java.util.List"
%>
<%@ page
import="java.util.Iterator" %>
<%@ page
import="org.apache.commons.io.FilenameUtils"%>
<%@ page import="java.io.File"
%>
<%@ page
import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page
import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page
import="org.apache.commons.fileupload.*"%>
<center><table
border="2">
<tr><td><h1>Your
files uploaded
</h1></td></tr>
<%
boolean isMultipart =
ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new
DiskFileItemFactory();
ServletFileUpload upload = new
ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
} else {
try {
String itemName = item.getName();
itemName=FilenameUtils.getName(itemName);
File savedFile = new
File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+itemName);
item.write(savedFile);
out.println("<tr><td><b>Your file has been saved
at the " +
"loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
%>
</table>
</center>
</body>
</html>
No comments:
Post a Comment