jspSmartUpload多文件上传

本文介绍了如何使用jspSmartUpload组件进行多文件上传。首先提供了下载地址www.jspsmart.com,然后详细说明了将jsmartcom_zh_CN.jar添加到工程lib目录的步骤,特别提到为了处理中文图片名乱码问题,临时将页面编码设置为GBK,原本为utf-8。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jspSmartUpload下载地址:www.jspsmart.com

将jsmartcom_zh_CN.jar配置在工程的lib文件中

实现如下:

jsp文档内容:(为解决上传的中文图片名乱码问题,暂时将编码改为"GBK",原为"utf-8")

<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk" pageEncoding="gbk"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk">
    <title>文件上传</title>
	<script type="text/javascript">
		function addFile() {
			var upFile = '<input type="file" name="myfile"><br><br>';
			document.getElementById("files")
					.insertAdjacentHTML("beforeEnd", upFile);
		}
	</script>
	</head>

	<body>
	<form action="./upload.do" method="post" enctype="multipart/form-data">
	<div id="files"><input type="file" name="myfile"/><br><br></div>
	<input type="button" value="增加文件" onclick="addFile()"> 
	<input type="submit" value="提交"/>${res}
	</form>
	</body>
	</html>

servlet类内容如下:

package servlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class SmartUploadServelt
 */
//使用注解不需要在xml文件配置  /upload.do   servlet注册
@WebServlet({ "/SmartUploadServelt", "/upload.do" })
public class SmartUploadServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final int maxFileSize = 1024*1024*10;//10M  
    private static final int totalMaxFileSize = 1024*1024*100;//100M  
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置上传文件保存路径
		String filePath = getServletContext().getRealPath("/")+"images";
		System.out.println("filePath:"+filePath);
		//将路径传到文件,如果文件不存在,则创建文件
		//File file = new File("E:/images/");
		File file = new File(filePath);
		if(!file.exists()){
			file.mkdirs();
		}
		
		SmartUpload su = new SmartUpload();//实例化smartupload
		/**初始化对象
		 * 在使用getServletConfig时候提示如下错误:
		 * The type javax.servlet.jsp.PageContext cannot be resolved. 
		 * It is indirectly referenced from required .class files
		 * 
		 * 把servlet-api.jar和jsp-api.jar包加入libraries中即可。
		 */
		su.initialize(getServletConfig(), request, response);
		//设置上传文件大小
		su.setMaxFileSize(maxFileSize);
		//设置所有文件大小
		su.setTotalMaxFileSize(totalMaxFileSize);
		//设置允许上传文件类型
		su.setAllowedFilesList("txt,png,jpg,gif");
		String result = "上传成功!";
		
		try {
			//设置禁止上传文件类型
			su.setDeniedFilesList("rar,jsp");
			//上传文件
			su.upload();
			//int count = su.save("E:/images/");
			
//			 filePath = su.getFiles().getFile(0).getFilePathName();//获取文件名
//			 filePath = new String(filePath.getBytes("GBK"),"utf-8");
	        
			int count = su.save(filePath);
			System.out.println("上传成功" + count + "个文件!");
		} catch (SQLException | SmartUploadException e) {
			result = "上传失败!";
			e.printStackTrace();
		}
		request.setAttribute("res", result);
		request.getRequestDispatcher("uploadFile.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

在使用getServletConfig时候提示错误在代码注释中有解决办法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值