jsp批量文件上傳
Ⅰ 怎麼在 jsp 頁面中上傳文件
使用jsp smartupload
示例:部分文件代碼 具體實現 找些教材
UploadServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;
import java.text.*;
import java.util.*;
/*******************************************************/
/* 該實例中盡可能多地用到了一些方法,在實際應用中 */
/* 我們可以根據自己的需要進行取捨! */
/*******************************************************/
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 新建一個SmartUpload對象,此項是必須的
SmartUpload myupload = new SmartUpload();
// 初始化,此項是必須的
ServletConfig config = getServletConfig();
myupload.initialize(config,request,response);
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.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對象
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName,memo;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 逐一提取上傳文件信息,同時可保存文件
for (int i=0;i<count;i++)
{
//取得一個上傳文件
File file = myupload.getFiles().getFile(i);
// 若文件不存在則繼續
if (file.isMissing()) continue;
// 取得文件名
fileName = file.getFileName();
// 取得文件全名
filePathName = file.getFilePathName();
// 取得文件擴展名
fileExtName = file.getFileExt();
// 取得隨機文件名
dt = new Date(System.currentTimeMillis());
Thread.sleep(100);
rndFilename= fmt.format(dt)+"."+fileExtName;
memo = myRequest.getParameter("memo"+i);
// 顯示當前文件信息
out.println("第"+(i+1)+"個文件的文件信息:<br>");
out.println(" 文件名為:"+fileName+"<br>");
out.println(" 文件擴展名為:"+fileExtName+"<br>");
out.println(" 文件全名為:"+filePathName+"<br>");
out.println(" 文件大小為:"+file.getSize()+"位元組<br>");
out.println(" 文件備注為:"+memo+"<br>");
out.println(" 文件隨機文件名為:"+rndFilename+"<br><br>");
// 將文件另存,以WEB應用的根目錄作為上傳文件的根目錄
file.saveAs("/upload/" + rndFilename,myupload.SAVE_VIRTUAL);
}
out.println(count+"個文件上傳成功!<br>");
}catch(Exception ex){
out.println("上傳文件超過了限制條件,上傳失敗!<br>");
out.println("錯誤原因:<br>"+ex.toString());
}
out.flush();
out.close();
}
}
Ⅱ 奼備竴涓猨sp鍙浠ュ氭枃浠朵笂浼犵殑渚嬪瓙
嫻佽岀殑jspSmartUpload鑳藉疄鐜頒竴嬈℃т笂浼犲氫釜鏂囦歡銆備笅闈㈡槸鎴戞壘鐨勪竴涓鍒楀瓙錛屼綘鍙浠ョ湅鐪嬩竴嬈′笂浼犲氫釜鏂囦歡鐨刯ava浠g爜濡備綍鍐欑殑錛屽笇鏈涘逛綘鏈夊府鍔╋紙鎴戜笉澶浼歫sp錛岃繖鍙鏄涓涓瀛jsp鐨勬湅鍙嬭磋繖涓鎻掍歡濂界敤錛屼綘鍙浠ョ爺絀剁爺絀訛級錛
Ⅲ 用jsp 怎樣實現文件上傳
你下載一個jspsmart組件,網上很容易下到,用法如下,這是我程序的相關片斷,供你參考: <%@ page import="com.jspsmart.upload.*" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String photoname="photoname";
// Variables
int count=0; // Initialization
mySmartUpload.initialize(pageContext); // Upload
mySmartUpload.upload();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ // Retreive the current file
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); // Save it only if this file exists
if (!myFile.isMissing()) {
java.util.Date thedate=new java.util.Date();
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
photoname = df.format(thedate);
photoname +="."+ myFile.getFileExt();
myFile.saveAs("/docs/docimg/" + photoname);
count ++; } }
%>
<% String title="1";
String author="1";
String content="1";
String pdatetime="1";
String topic="1";
String imgintro="1";
String clkcount="1"; if(mySmartUpload.getRequest().getParameter("title")!=null){
title=(String)mySmartUpload.getRequest().getParameter("title");
title=new String(title.getBytes("gbk"),"ISO-8859-1");
}
if(mySmartUpload.getRequest().getParameter("author")!=null){
author=(String)mySmartUpload.getRequest().getParameter("author");
author=new String(author.getBytes("gbk"),"ISO-8859-1");
}
if(mySmartUpload.getRequest().getParameter("content")!=null){
content=(String)mySmartUpload.getRequest().getParameter("content");
content=new String(content.getBytes("gbk"),"ISO-8859-1");
}
if(mySmartUpload.getRequest().getParameter("pdatetime")!=null){
pdatetime=(String)mySmartUpload.getRequest().getParameter("pdatetime");
}
if(mySmartUpload.getRequest().getParameter("topic")!=null){
topic=(String)mySmartUpload.getRequest().getParameter("topic");
}
if(mySmartUpload.getRequest().getParameter("imgintro")!=null){
imgintro=(String)mySmartUpload.getRequest().getParameter("imgintro");
imgintro=new String(imgintro.getBytes("gbk"),"ISO-8859-1");
}
if(mySmartUpload.getRequest().getParameter("clkcount")!=null){
clkcount=(String)mySmartUpload.getRequest().getParameter("clkcount");
}
//out.println(code+name+birthday);
%>
Ⅳ jsp 如何實現文件上傳和下載功能
上傳:
MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub
FormFile fname=mf.getFname();
byte [] fn = fname.getFileData();
OutputStream out = new FileOutputStream("D:\"+fname.getFileName());
Date date = new Date();
String title = fname.getFileName();
String url = "d:\"+fname.getFileName();
Upload ul = new Upload();
ul.setDate(date);
ul.setTitle(title);
ul.setUrl(url);
UploadDAO uld = new UploadDAO();
uld.save(ul);
out.write(fn);
out.close();
下載:
DownloadForm downloadForm = (DownloadForm)form;
String fname = request.getParameter("furl");
FileInputStream fi = new FileInputStream(fname);
byte[] bt = new byte[fi.available()];
fi.read(bt);
//設置文件是下載還是打開以及打開的方式msdownload表示下載;設置字湖集,//主要是解決文件中的中文信息
response.setContentType("application/msdownload;charset=gbk");
//文件下載後的默認保存名及打開方式
String contentDisposition = "attachment; filename=" + "java.txt";
response.setHeader("Content-Disposition",contentDisposition);
//設置下載長度
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
return null;
Ⅳ JSP頁面使用了enctype="multipart/form-data" 上傳文件的問題
s:file控制項是個一拖三的玩意,一個file1的控制項對應了三個Action類中的屬性:file1,
file1ContentType和file1FileName。file1的類型是File,它對應的值是文件上傳後臨時存放
的位置,contenttype指文件類型,FileName指文件名稱。
批量上傳請使用List
Ⅵ jsp+servlet實現文件上傳問題
try{
//1.獲得上傳幫助類SmartUpload SmartUpload su = new SmartUpload();
//2.設置上傳允許大小,以B為單位 su.setMaxFileSize(3*1024*1024);
//3.設置允許上傳圖片格式 su.setAllowedFilesList("jpg,gif,bmp");
//4.初始化
su.initialize(this.getServletConfig(), request, response);
//上傳
su.upload();
//獲取上傳文件的入口,客戶端會認為上傳是一堆文件 Files files = su.getFiles();
//上傳一個,從0開始
File file = files.getFile(0);
//上傳文件的名字
String fileName = file.getFileName();
//上傳文件的後綴名
String fileExt = file.getFileExt();
//上傳文件的大小
int fileSize = file.getSize();
//獲取頁面提交的值,此處的request不再是HttpServletRequest,必須使用SmartUpload提供的request
Request myreq = su.getRequest();
String username = myreq.getParameter("username");
String password = myreq.getParameter("password");
//設置上傳圖片的路徑地址
java.io.File truefile = new java.io.File(this.getServletContext().getRealPath("/image"));
//不存在則創建
if(!truefile.exists()){
truefile.mkdir();
}
//對圖片進行重命名 String trueFileName = new UUIDGenerator().generate().toString()+"."+fileExt;
//最終路徑/image/435454343.jpg
String finalPath = "/"+truefile.getName()+"/"+trueFileName;
file.saveAs(finalPath);
Ⅶ jsp文件上傳
public class MultipartTestServlet extends HttpServlet {
public MultipartTestServlet() { //構造方法
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) //servlet的doPost方法處理POST請求
throws ServletException, IOException { //拋出異常
request.setCharacterEncoding("gbk"); //設置字元為GBK
RequestContext requestContext = new ServletRequestContext(request); //實例化RequestContext對象
if(FileUpload.isMultipartContent(requestContext)){
//判斷是否包含 multipart 內容
DiskFileItemFactory factory = new DiskFileItemFactory();
// 創建基於磁碟的文件工廠
factory.setRepository(new File("c:/tmp/")); // 設置臨時目錄
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("gbk");
upload.setSizeMax(2000000); //設置緩沖區大小
List items = new ArrayList();
try {
items = upload.parseRequest(request); // 得到所有的文件
} catch (FileUploadException e1) {
System.out.println("文件上傳發生錯誤" + e1.getMessage());
}
Iterator it = items.iterator();
while(it.hasNext()){
FileItem fileItem = (FileItem) it.next();
if(fileItem.isFormField()){
System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk")); //獲得表單中域的名字。獲得從瀏覽器中取得的文件全路徑
}else{
System.out.println(fileItem.getFieldName() + " " +
fileItem.getName() + " " +
fileItem.isInMemory() + " " +
fileItem.getContentType() + " " +
fileItem.getSize());
if(fileItem.getName()!=null && fileItem.getSize()!=0){
// 瀏覽器中取得的文件全路徑不為空 大小 不為0 則寫入
File fullFile = new File(fileItem.getName());
File newFile = new File("c:/temp/" + fullFile.getName());
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("文件沒有選擇 或 文件內容為空");
}
}
}
}
}
}
Ⅷ jsp涓濡備綍澧炲姞涓婁紶鏂囦歡鐨勫姛鑳斤紵浠g爜錛
棣栧厛涓嬭澆jspsmartupload緇勪歡
http://www.jspsmart.com
浜屻佸皢鐩褰昷spsmartupload/wib_inf/classes涓鐨勫唴瀹規嫹璐濆埌緗戠珯鎵鍦ㄧ殑瀹為檯鐩褰曚腑鐨刉EB-INF涓錛坮esin鏄榪欎釜鐩褰曪紝鍏朵粬鐨勫彲鑳芥槸classes,鍏蜂綋璇鋒煡闃卝spsmartupload/help/setup.htm)
涓夈佸亣濡傛槸resin榪愯孞SP錛岃峰湪resin鐨刢onf/resin.conf涓鐨
<web-app> 鍜 </web-app> 涓鍔犲叆錛
<path-mapping url-pattern=鈥/upload/*鈥 real-path=鈥檉:\jsp\jspsmartupload\upload鈥/>
鍥涖佷笂浼犵晫闈㈢殑浠g爜濡備笅錛(鏂囦歡鍚嶏細insert.htm)
<FORM METHOD="POST" ACTION=" uploadfile.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"> <BR>
<INPUT TYPE="FILE" NAME="FILE2" SIZE="50"> <BR>
<INPUT TYPE="FILE" NAME="FILE3" SIZE="50"> <BR>
<INPUT TYPE="FILE" NAME="FILE4" SIZE="50"> <BR>
涓婚橈細 <input type="text" name="text1" > <br>
<INPUT type=submit value=鍐 瀹 name=ok>
</form>
娉ㄩ噸涓婇潰鐨剅eal-path鐩褰
浜斻乽ploadfile.jsp鐨勪唬鐮佸備笅:
<錛匑page contentType="text/html;charset=gb2312"
language="java"
import="com.jspsmart.upload.*"錛>
<jsp:useBean id="mySmartUpload"
scope="page"
class="com.jspsmart.upload.SmartUpload" />