JSP中上传图片 完整代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
upfile.jsp
uploadimage.jsp
uploadimage.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ page
import="java.io.*,java.awt.image.*,com.sun.image.codec.jpeg.*,jav a.sql.*,com.jspsmart.upload.*,java.util.*"%>
<%@ page import="mainClass.*"%>
My JSP'uploadimage.jsp' starting page <%
S martUpload sma = new SmartUpload();
l ong file_max_size = 4000000;
S tring filename1 = "", ext = "", testvar = "";
S tring url = "uploadfiles/";
s ma.initialize(pageContext);
t ry
{
sma.setAllowedFilesList("jpg,gif");
sma.upload();
}catch (Exception e) {
%>
alert("只允许上传jpg,gif格式图片");
window.location.href="upfile.jsp"
<%
}
try {
com.jspsmart.upload.File myf = sma.getFiles().getFile(0);
if (myf.isMissing()) {
%>
alert("请选择要上传的文件!");
window.location.href="upfile.jsp"
<%
} else {
ext = myf.getFileExt();
int file_size = myf.getSize();
String saveurl = "";
if (file_size < file_max_size) {
Calendar cal = Calendar.getInstance();
String filename = String.valueOf(cal.getTimeInMills());
saveurl = request.getRealPath("/") + url;
saveurl += filename + "." + ext;
myf.saveAs(saveurl, sma.SAVE_PHYSICAL);
myclass mc = new myclass(request
.getRealPath("data/data.mdb"));
mc
.executeInsert("insert into [path] values('uploadfiles/"
+ filename + "." + ext + "')");//检查
out.println("图片上传成功!");
response.sendRedirect("showimg.jsp");
}
}
} catch (Exception e) {
e.printStackTrace();
}
%>