jsp文件上傳與下載實(shí)例代碼
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();//上傳文件,此項(xiàng)是必須的
int count = myupload.getFiles().getCount();
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
//一一提取上傳文件信息,同時(shí)可保持文件
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)+"個(gè)文件的文件信息:<br>");
out.println(" 文件名為:"+fileName+"<br>");
out.println(" 文件擴(kuò)展名為:"+fileExtName+"<br>");
out.println(" 文件全名:"+filePathName+"<br>");
out.println(" 文件大?。?+file.getSize()/1024+"kb<br>");
//創(chuàng)建文件保存位置
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.*" %>
<%
//設(shè)定請(qǐng)求編碼方式,否則遇到中文就會(huì)亂碼
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>上傳文件實(shí)例</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>上傳文件實(shí)例</h2>
請(qǐng)選擇上傳文件數(shù)量:
<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");//設(shè)置為下載application/x-download
String filedownload = "/要下載的文件名";//即將下載的文件的相對(duì)路徑
String filedisplay = "最終要顯示給用戶的保存文件名";//下載文件時(shí)顯示的文件保存名稱
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頁(yè)面實(shí)現(xiàn)文件上傳下載類代碼
- jsp中點(diǎn)擊圖片彈出文件上傳界面及預(yù)覽功能的實(shí)現(xiàn)
- jsp實(shí)現(xiàn)文件上傳下載的程序示例
- Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)
- AJAX和JSP實(shí)現(xiàn)的基于WEB的文件上傳的進(jìn)度控制代碼
- jsp中點(diǎn)擊圖片彈出文件上傳界面及實(shí)現(xiàn)預(yù)覽實(shí)例詳解
- 利用jsp+Extjs實(shí)現(xiàn)動(dòng)態(tài)顯示文件上傳進(jìn)度
- jsp 文件上傳瀏覽,支持ie6,ie7,ie8
- servlet+JSP+mysql實(shí)現(xiàn)文件上傳的方法
- JSP實(shí)現(xiàn)文件上傳功能
相關(guān)文章
Jsp連接Access數(shù)據(jù)庫(kù)(不通過(guò)建立ODBC數(shù)據(jù)源的方法)
Jsp連接Access數(shù)據(jù)庫(kù)(不通過(guò)建立ODBC數(shù)據(jù)源的方法)...2007-02-02JSP多種web應(yīng)用服務(wù)器導(dǎo)致JSP源碼泄漏漏洞
JSP多種web應(yīng)用服務(wù)器導(dǎo)致JSP源碼泄漏漏洞...2006-10-10jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析(附源碼)
這篇文章主要介紹了jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析,xml做數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶登錄與注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11JSP中常用的JSTL fmt(format格式化)標(biāo)簽用法整理
這篇文章主要介紹了JSP中常用的JSTL fmt(format格式化)標(biāo)簽用法整理,fmt的格式化處理遵循i18n國(guó)際化格式標(biāo)準(zhǔn),需要的朋友可以參考下2016-04-04jsp+mysql數(shù)據(jù)庫(kù)操作常用方法實(shí)例總結(jié)
這篇文章主要介紹了jsp+mysql數(shù)據(jù)庫(kù)操作常用方法,以實(shí)例形式較為詳細(xì)的總結(jié)了JSP操作mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)基本的增、刪、改、查相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10JSP使用JDBC完成動(dòng)態(tài)驗(yàn)證及采用MVC完成數(shù)據(jù)查詢的方法
這篇文章主要介紹了JSP使用JDBC完成動(dòng)態(tài)驗(yàn)證及采用MVC完成數(shù)據(jù)查詢的方法,實(shí)例分析了JDBC連接數(shù)據(jù)庫(kù)及查詢數(shù)據(jù)庫(kù)的實(shí)現(xiàn)技巧,并結(jié)合MVC框架進(jìn)行分析說(shuō)明,需要的朋友可以參考下2015-09-09servlet+JSP+mysql實(shí)現(xiàn)文件上傳的方法
這篇文章主要介紹了servlet+JSP+mysql實(shí)現(xiàn)文件上傳的方法,涉及JSP文件傳輸與判斷及數(shù)據(jù)庫(kù)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
今天小編就為大家分享一篇關(guān)于在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03