当前位置:首页 » 文件管理 » jsp批量文件上传

jsp批量文件上传

发布时间: 2024-07-30 06:54:33

Ⅰ 怎么在 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" />

热点内容
php解压程序 发布:2025-01-15 17:06:22 浏览:140
刷助力脚本 发布:2025-01-15 17:02:31 浏览:518
c盘里的用户文件夹可以删除 发布:2025-01-15 16:56:45 浏览:949
虚幻4编译到哪里 发布:2025-01-15 16:50:19 浏览:754
透明度渐变android 发布:2025-01-15 16:45:08 浏览:833
dos连接oracle数据库 发布:2025-01-15 16:41:39 浏览:904
网络配置比较低怎么做 发布:2025-01-15 16:35:38 浏览:361
android弹出键盘监听 发布:2025-01-15 16:35:11 浏览:207
uz画图编程 发布:2025-01-15 16:32:44 浏览:883
ppt怎么解压 发布:2025-01-15 16:14:58 浏览:849