導航:首頁 > 編程語言 > jsp怎麼實現動態上傳多文件上傳

jsp怎麼實現動態上傳多文件上傳

發布時間:2025-09-04 21:32:24

㈠ 如何上傳多個文件

很簡單,你把要上傳的文件全部用滑鼠框起來,然後在隨便一個選中的圖標上點右鍵選擇「添加到壓縮文件」就可以了。
還可以給你的壓縮文件命名。

㈡ 請高手給一個js多文件上傳的例子(必須兼容IE)解決追加50分。請看補充。

一、Servlet實現文件上傳,需要添加第三方提供的jar包
下載地址:
1) commons-fileupload-1.2.2-bin.zip: 點擊打開鏈接

2) commons-io-2.3-bin.zip: 點擊打開鏈接
接著把這兩個jar包放到 lib文件夾下:

二:文件上傳的表單提交方式必須是POST方式,
編碼類型:enctype="multipart/form-data",默認是 application/x-www-form-urlencoded
比如:
<form action="FileUpLoad"enctype="multipart/form-data"method="post">

三、舉例:
1.fileupload.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'fileuploadjsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="stylescss">
-->

</head>

<body>
<!-- enctype 默認是 application/x-www-form-urlencoded -->
<form action="FileUpLoad" enctype="multipart/form-data" method="post" >

用戶名:<input type="text" name="usename"> <br/>
上傳文件:<input type="file" name="file1"><br/>
上傳文件: <input type="file" name="file2"><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

2.實際處理文件上傳的 FileUpLoad.java
package comservletfileupload;
import javaioFile;
import javaio*;
import javaioIOException;
import javaioPrintWriter;
import javautilList;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import ;
import ;
import ;
import ;
import ;
import ;

/**
*
* @author Administrator
* 文件上傳
* 具體步驟:
* 1)獲得磁碟文件條目工廠 DiskFileItemFactory 要導包
* 2) 利用 request 獲取 真實路徑 ,供臨時文件存儲,和 最終文件存儲 ,這兩個存儲位置可不同,也可相同
* 3)對 DiskFileItemFactory 對象設置一些 屬性
* 4)高水平的API文件上傳處理 ServletFileUpload upload = new ServletFileUpload(factory);
* 目的是調用 parseRequest(request)方法 獲得 FileItem 集合list ,
*
* 5)在 FileItem 對象中 獲取信息, 遍歷, 判斷 表單提交過來的信息 是否是 普通文本信息 另做處理
* 6)
* 第一種 用第三方 提供的 itemwrite( new File(path,filename) ); 直接寫到磁碟上
* 第二種 手動處理
*
*/
public class FileUpLoad extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

requestsetCharacterEncoding("utf-8"); //設置編碼

//獲得磁碟文件條目工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
//獲取文件需要上傳到的路徑
String path = requestgetRealPath("/upload");

//如果沒以下兩行設置的話,上傳大的 文件 會佔用 很多內存,
//設置暫時存放的 存儲室 , 這個存儲室,可以和 最終存儲文件 的目錄不同
/**
* 原理 它是先存到 暫時存儲室,然後在真正寫到 對應目錄的硬碟上,
* 按理來說 當上傳一個文件時,其實是上傳了兩份,第一個是以 tem 格式的
* 然後再將其真正寫到 對應目錄的硬碟上
*/
factorysetRepository(new File(path));
//設置 緩存的大小,當上傳文件的容量超過該緩存時,直接放到 暫時存儲室
factorysetSizeThreshold(1024*1024) ;

//高水平的API文件上傳處理
ServletFileUpload upload = new ServletFileUpload(factory);

try {
//可以上傳多個文件
List<FileItem> list = (List<FileItem>)uploadparseRequest(request);

for(FileItem item : list)
{
//獲取表單的屬性名字
String name = itemgetFieldName();

//如果獲取的 表單信息是普通的 文本 信息
if(itemisFormField())
{
//獲取用戶具體輸入的字元串 ,名字起得挺好,因為表單提交過來的是 字元串類型的
String value = itemgetString() ;

requestsetAttribute(name, value);
}
//對傳入的非 簡單的字元串進行處理 ,比如說二進制的 圖片,電影這些
else
{
/**
* 以下三步,主要獲取 上傳文件的名字
*/
//獲取路徑名
String value = itemgetName() ;
//索引到最後一個反斜杠
int start = valuelastIndexOf("\\");
//截取 上傳文件的 字元串名字,加1是 去掉反斜杠,
String filename = valuesubstring(start+1);

requestsetAttribute(name, filename);

//真正寫到磁碟上
//它拋出的異常 用exception 捕捉

//itemwrite( new File(path,filename) );//第三方提供的

//手動寫的
OutputStream out = new FileOutputStream(new File(path,filename));

InputStream in = itemgetInputStream() ;

int length = 0 ;
byte [] buf = new byte[1024] ;

Systemoutprintln("獲取上傳文件的總共的容量:"+itemgetSize());

// inread(buf) 每次讀到的數據存放在 buf 數組中
while( (length = inread(buf) ) != -1)
{
//在 buf 數組中 取出數據 寫到 (輸出流)磁碟上
outwrite(buf, 0, length);

}

inclose();
outclose();
}
}

} catch (FileUploadException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block

//eprintStackTrace();
}

requestgetRequestDispatcher("filedemojsp")forward(request, response);

}

}

System.out.println("獲取上傳文件的總共的容量:"+item.getSize());

3.filedemo.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'filedemojsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="stylescss">
-->

</head>

<body>

用戶名:${requestScopeusename } <br/>
文件:${requestScopefile1 }<br/>
${requestScopefile2 }<br/>
<!-- 把上傳的圖片顯示出來 -->
<img alt="go" src="upload/<%=(String)requestgetAttribute("file1")%> " />

</body>
</html>

4結果頁面:

以上就是本文的全部

㈢ java實現文件上傳,代碼盡量簡潔~~~~~·

一個SpringMVC的文件上傳示例
1、jsp頁面代碼
<%@pagepageEncoding="utf-8"%>

<!DOCTYPEhtml>

<html>

<head>

<metacharset="utf-8">

<title>上傳圖片</title>

</head>

<body>

<formaction="message.do">

<inputtype="text"name="test"value="Submit"/><inputtype="submit"

value="Submit"/>

</form>

<formaction="upload.do"method="post"enctype="multipart/form-data">

<inputtype="file"name="file"/><inputtype="submit"value="Submit"/>

</form>

</body>

</html>

2、action代碼
@RequestMapping(value="/upload.do")

publicStringupload(@RequestParam(value="file",required=false)MultipartFilefile,HttpServletRequestrequest,

ModelMapmodel){

Stringpath="/Users/xieyuhai/Desktop";//更換一下路徑

StringfileName=file.getOriginalFilename();

//StringfileName=newDate().getTime()+".jpg";

System.out.println(path);

FiletargetFile=newFile(path,fileName);

if(!targetFile.exists()){

targetFile.mkdirs();

}

//保存

try{

file.transferTo(targetFile);

}catch(Exceptione){

e.printStackTrace();

}

model.addAttribute("resultUrl",request.getContextPath()+"/upload/"+fileName);

return"result";

}

㈣ JSP做的簡單的文件上傳下載代碼

暈,給你寫完了,人怎麼沒了呢……

閱讀全文

與jsp怎麼實現動態上傳多文件上傳相關的資料

熱點內容
win10放文件的格子怎麼弄 瀏覽:664
魅藍note升級flyme45 瀏覽:353
怎麼讓文件夾內容按序列 瀏覽:408
傳文件到電腦但找不到手機文件夾 瀏覽:943
手機程序禁止自起 瀏覽:268
301轉向代碼 瀏覽:79
蘋果6進水有水印換個屏幕多少 瀏覽:322
原神遊戲賬號數據在文件的哪個地方 瀏覽:996
js算拖動的坐標 瀏覽:33
pscs5裁剪工具 瀏覽:852
大數據產生的技術原因包括哪些 瀏覽:264
exchange公共文件夾 瀏覽:292
處方取消代碼 瀏覽:311
微信怎麼切換零錢支付 瀏覽:221
qq男圖片大全 瀏覽:508
蘋果4s國行是什麼版本 瀏覽:226
財富小店2安卓版 瀏覽:473
js如何記錄視頻播放時長 瀏覽:487
jsp怎麼實現動態上傳多文件上傳 瀏覽:170
psjsx文件 瀏覽:854

友情鏈接