色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

jsp文件上傳與下載實例代碼

文件上傳
復制代碼 代碼如下:
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>處理上傳的文件</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();//上傳文件,此項是必須的
int count = myupload.getFiles().getCount();
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
//一一提取上傳文件信息,同時可保持文件
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");//獲取文件名
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("第"+(i+1)+"個文件的文件信息:<br>");
out.println(" 文件名為:"+fileName+"<br>");
out.println(" 文件擴展名為:"+fileExtName+"<br>");
out.println(" 文件全名:"+filePathName+"<br>");
out.println(" 文件大小:"+file.getSize()/1024+"kb<br>");
//創建文件保存位置
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(" 文件保存路徑:"+savePath);
file.saveAs(savePath+"/"+rndFilename,SmartUpload.SAVE_PHYSICAL);
}

} catch (Exception e) {
out.println("文件上傳失敗!!!!");
e.printStackTrace();
}
out.flush();
out.close();

}
}

復制代碼 代碼如下:
<%@ page language="Java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//設定請求編碼方式,否則遇到中文就會亂碼
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>上傳文件實例</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>上傳文件實例</h2>
請選擇上傳文件數量:
<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="提交"/>
</form>
</body>
</html>

文件下載
復制代碼 代碼如下:
<%
response.setContentType("application/x-download");//設置為下載application/x-download
String filedownload = "/要下載的文件名";//即將下載的文件的相對路徑
String filedisplay = "最終要顯示給用戶的保存文件名";//下載文件時顯示的文件保存名稱
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
{
}
%>

jsp技術jsp文件上傳與下載實例代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 东至县| 太和县| 平泉县| 靖远县| 教育| 西藏| 凯里市| 德令哈市| 晋州市| 库伦旗| 三原县| 惠来县| 密山市| 札达县| 沈阳市| 巩留县| 黄山市| 五家渠市| 郑州市| 江孜县| 尉犁县| 贺兰县| 小金县| 安国市| 泾川县| 紫金县| 大渡口区| 攀枝花市| 泰州市| 彰化市| 白城市| 固安县| 江西省| 连南| 灯塔市| 正蓝旗| 北票市| 乌海市| 延庆县| 西青区| 通江县|