SmartUpload文件上传

实现思路和关键代码

使用SmartUpload组件上传文件并进行控制

使用com.jspsmart.upload.Request对象获取表单提交数据

调用封装业务的JavaBean将数据保存至数据库

  • SmartUpload 获取表单数据

  • getRequest():得到com.jspsmart.upload.Request对象

  • 常用方法

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>Insert title here</title>
</head>
<body>
	<img alt="" src="onload/by.jpg">
	<hr>
	<form action="dosendFile.jsp" enctype="multipart/form-data" method="post">
		<input type="file" name="myfile"><br>
		<input type="submit" value="开始上传">
	</form>

</body>
</html>
  • 编写上传文件处理页uploadManage.jsp

<%@page import="com.jspsmart.upload.File"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//创建SmartUpload对象
	SmartUpload su = new SmartUpload();
	//初始化
	su.initialize(pageContext);
	//声明一个File对象 用来接收上传的文件
	File file = null;
	//设置允许上传的文件类型
	su.setAllowedFilesList("jpg,png,gif,");
	//设置不允许上传的文件类型
	su.setDeniedFilesList("bat,exe,mp4");
	//设置单文件大小
	su.setMaxFileSize(40000);
	//设置总文件大小
	su.setTotalMaxFileSize(50000);
	//设置编码
	su.setCharset("utf-8");
	//开始上传
	su.upload();	
	//获取文件集合中的第一个文件
	file = su.getFiles().getFile(0);
	String filePath = "";
	if(!file.isMissing()){//判断是否被选中
		//拼接文件上传到服务器的 路径
		filePath = "onload/"+file.getFileName();
		//上传到服务器 保存到指定路径
		file.saveAs(filePath,SmartUpload.SAVE_VIRTUAL);
	}
	out.print("上传成功");
	out.print("<img src='"+filePath+"'>");
	
%>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值