jsp file upload and download the instance code
- 2020-06-12 10:15:55
- OfStack
File upload:
File Download:
public class UploadServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
SmartUpload myupload = new SmartUpload();
ServletConfig config = getServletConfig();
myupload.initialize(config,req,resp);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("<h2> Process the uploaded files </h2>");
out.println("<hr>");
try {
myupload.setMaxFileSize(1024*1024);
myupload.setTotalMaxFileSize(5*1024*1024);
myupload.setAllowedFilesList("doc,txt,jpg,gif");
myupload.setDeniedFilesList("exe,bat,jsp,htm,html");
myupload.upload();// Upload file, this is required
int count = myupload.getFiles().getCount();
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
//11 Extract the uploaded file information while keeping the file
for(int i = 0; i < count; i++){
File file = myupload.getFiles().getFile(i);
if(file.isMissing())
continue;
fileName = new String(file.getFileName().getBytes(),"utf-8");// Get file name
filePathName = new String(file.getFilePathName().getBytes(),"utf-8");
fileExtName = file.getFileExt();
dt = new Date(System.currentTimeMillis());
Thread.sleep(100);
rndFilename = fmt.format(dt)+i+"."+fileExtName;
out.println(" The first "+(i+1)+" File information of each file: <br>");
out.println(" File name: "+fileName+"<br>");
out.println(" File extension: "+fileExtName+"<br>");
out.println(" Full name: "+filePathName+"<br>");
out.println(" File size: "+file.getSize()/1024+"kb<br>");
// Create a file save location
ServletContext context = req.getServletContext();
String savePath = context.getRealPath("/upload/");
java.io.File folder = new java.io.File(savePath);
if(!folder.exists()){
folder.mkdirs();
}
out.println(" File saving path: "+savePath);
file.saveAs(savePath+"/"+rndFilename,SmartUpload.SAVE_PHYSICAL);
}
} catch (Exception e) {
out.println(" File upload failed!! ");
e.printStackTrace();
}
out.flush();
out.close();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
// Set the request code, otherwise Chinese will be confused
request.setCharacterEncoding("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> Upload file instance </title>
<script type="text/javascript" src="js/jquery-1.8.1.js"></script>
<script type="text/javascript">
$(function(){
$("#number").change(function(){
var number = $("#number").attr("value");
$("#files").html("");
for(var i = 0; i < number; i++){
$("#files").append("<input type='file' name='file''"+i+"'><br/>");
}
});
});
</script>
</head>
<body>
<h2> Upload file instance </h2>
Please select the number of files to upload:
<select id="number">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<div id="files"></div>
<input type="submit" value=" submit "/>
</form>
</body>
</html>
File Download:
<%
response.setContentType("application/x-download");// Set to Download application/x-download
String filedownload = "/ The file name to download ";// The relative path of the file to be downloaded
String filedisplay = " The final save file name to display to the user ";// Save the name of the file shown when downloading the file
String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
try
{
RequestDispatcher dis = application.getRequestDispatcher(filedownload);
if(dis!= null)
{
dis.forward(request,response);
}
response.flushBuffer();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
}
%>