File Upload:
Copy Code code as follows:
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 ("Out.println ("
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 item is required
int count = Myupload.getfiles (). GetCount ();
Request myrequest = Myupload.getrequest ();
String Rndfilename,fileextname,filename,filepathname;
Date dt = null;
SimpleDateFormat FMT = new SimpleDateFormat ("YyyyMMdd");
One by one extract uploaded file information, while maintaining 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 filename
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) + "Document Information:<br>");
Out.println ("File name:" +filename+ "<br>");
Out.println ("File name extension:" +fileextname+ "<br>");
Out.println ("File name:" +filepathname+ "<br>");
OUT.PRINTLN ("File Size:" +file.getsize ()/1024+ "kb<br>");
To 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 Save path:" +savepath);
File.saveas (savepath+ "/" +rndfilename,smartupload.save_physical);
}
catch (Exception e) {
Out.println ("File upload failed!!!! ");
E.printstacktrace ();
}
Out.flush ();
Out.close ();
}
}
Copy Code code as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "com.jspsmart.upload.*"%>
<%
Set the request encoding method, otherwise encounter Chinese will be garbled
Request.setcharacterencoding ("Utf-8");
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<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>
<body>
Please select the number of uploaded files:
<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= "submitted"/>
</form>
</body>
File Download:
Copy Code code as follows:
<%
Response.setcontenttype ("Application/x-download");/set to download Application/x-download
String filedownload = "/file name to download";//The relative path of the file you are about to download
String filedisplay = "Saved file name to be displayed to the user eventually";//File Save name displayed when downloading files
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
{
}
%>