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时候提示错误在代码注释中有解决办法