一、jsp文件上传与下载:
1、文件上传应用场景:添加附件(如邮件附件等);图片、视屏、音乐等。
2、文件上传组件:Smartupload(不成熟,不稳定)、fileupload。
3、fileupload文件上传原理图:

4、如何上传一个文件:
(1)引入fileupload包;
(2)编写html页面,注意表单属性;
(3)编写文件上传处理jsp。
(4)文件上传常用参数:SizeMax:设置文件上传最大字节数(单位:字节)
Upload.setSizeMax(4*1024*1024);//4M
5、表单文件上传弊端:文件上传服务器端无法判断大小;上传时无法显示上传进度。
6、文件上传安全漏洞防范:通过限制上传文件的后缀名(类似.jsp,.jspx,.JSP,大小写字母等),限制后缀名注意事项。
eg:文件上传:
(1)上传表单:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/4/5
Time: 17:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String realpath=session.getServletContext().getRealPath("");
%>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h2><%=realpath%></h2>
<form method="post" name="form1" action="upload.jsp" enctype="multipart/form-data">
<input type="file" name="file1" />
<button type="submit" name="btn">上传</button>
</form>
</body>
</html>
(2)upload.jsp:
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.io.IOUtils" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/4/6
Time: 16:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("Utf-8");
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setSizeThreshold(8*1024);//设置缓冲区大小
//定义upload组件
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setSizeMax(1024*1024*40);//设置文件大小
List<FileItem> items = upload.parseRequest(request);
String filename="";
String realpath="";
for(FileItem item:items){
if(!item.isFormField()){
realpath=session.getServletContext().getRealPath("");
FileOutputStream os=new FileOutputStream(realpath+ File.separator+item.getName());
filename=item.getName();
IOUtils.copy(item.getInputStream(),os);
os.flush();
os.close();
}
System.out.println(item.getFieldName());
}
%>
<p><%=realpath%></p>
<p><%=filename%></p>
<img src="<%=filename%>" width="200px"/>
(3)同(1)Ajax+html5文件上传(使用formData对象接受表单):
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/4/6
Time: 17:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
function upload(){
alert(1);
var fdata=new FormData($("#form1")[0]);
jQuery.ajax({
url:"upload.jsp",
type:"post",
data: fdata,
contentType:false,
processData:false,
success:function(resp){
$("#outdata").html(resp);
}
});
}
</script>
</head>
<body>
<form method="post" name="form1" id="form1" action="upload.jsp" enctype="multipart/form-data">
<input type="file" name="file1" />
<button type="button" id="btn" name="btn" onclick="upload()">上传</button>
</form>
<div id="outdata"></div>
</body>
</html>
本文主要探讨了JavaWeb中jsp文件的上传与下载操作,涉及文件上传的应用场景,如邮件附件,以及图片、视频等内容的上传。文章提到了Smartupload和fileupload两个上传组件,重点讲解了fileupload组件的上传原理和步骤,包括引入包、创建html表单、编写处理jsp等。此外,还指出了文件上传过程中可能存在的问题,如服务器端无法判断文件大小和无法显示上传进度,并提醒开发者注意文件上传的安全性,如限制上传文件的后缀名来防止恶意文件的上传。最后,文章给出了一个简单的上传表单例子,并提及了使用Ajax和HTML5的文件上传方式。
2232

被折叠的 条评论
为什么被折叠?



