加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

JspSmartUpload上传常见问题分析

发布时间:2023-07-18 14:20:16 所属栏目:教程 来源:
导读:因为手头的项目需要 上传图片,初次才用比较流行的jspSmartUpload上传。

毕竟没有搞过,弄了一个下午,终于将碰到的问题搞定了。

第一个问题,表单设置:method="post",ENCTYPE="multipart/form-data",只能这
因为手头的项目需要 上传图片,初次才用比较流行的jspSmartUpload上传。
 
毕竟没有搞过,弄了一个下午,终于将碰到的问题搞定了。
 
第一个问题,表单设置:method="post",ENCTYPE="multipart/form-data",只能这样
 
第二个问题,不能通过request.getParameter()取得取得其他的表单字段值,只能通过SmartUpload.getRequest().getParameter("")取得,否则得到的值始终为null
 
第三,SmartUpload.getRequest().getParameter("")中如果取得中文转马方式为
 
String name=new String(su.getRequest().getParameter("name").getBytes(),"UTF-8");
 
这是我试了许多方式,唯一正确的
 
第四 ,取得其他表单字段的值必要在 // 新建一个SmartUpload对象
 
SmartUpload su = new SmartUpload();
 
后面完成,否则出错
 
相关代码入下:希望对大家有所帮组
 
upload.jsp
 
<%@ page language="java" pageEncoding="UTF-8" import="java.sql.*,com.jspsmart.upload.*"%>
 
<jsp:useBean id="date" scope="request" class="util.DateAndTime"/>
 
<jsp:useBean id="data" scope="request" class="util.DataConnection"/>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html:html locale="true">
 
<head>
 
<html:base />
 
<title>upload.jsp</title>
 
<meta http-equiv="pragma" content="no-cache">
 
<meta http-equiv="cache-control" content="no-cache">
 
<meta http-equiv="expires" content="0">
 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 
<meta http-equiv="description" content="This is my page">
 
<!--
 
<link rel="stylesheet" type="text/css" href="styles.css">
 
-->
 
</head>
 
<body>
 
<%
 
// 新建一个SmartUpload对象
 
// 新建一个SmartUpload对象
 
SmartUpload su = new SmartUpload();
 
// 上传初始化
 
su.initialize(pageContext);
 
// 设定上传限制
 
// 1.限制每个上传文件的最大长度。
 
su.setMaxFileSize(10000);
 
// 2.限制总上传数据的长度。
 
su.setTotalMaxFileSize(10000);
 
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
 
su.setAllowedFilesList("gif,jpeg,jpg");
 
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
 
su.setDeniedFilesList("exe,bat,jsp,htm,html,asp,php,xml,xhtml");
 
// 上传文件
 
su.upload();
 
// 将上传文件全部保存到指定目录
 
su.save("/upload");
 
String pathname=null;
 
String name=new String(su.getRequest().getParameter("name").getBytes(),"UTF-8");
 
String link=su.getRequest().getParameter("link");
 
for(int i=0;i<su.getFiles().getCount();i++)
 
{
 
com.jspsmart.upload.File file = su.getFiles().getFile(i);
 
if(file.isMissing()){
 
continue;
 
}
 
pathname=file.getFileName();
 
}
 
String ptime=date.getDate2();
 
String sql="insert into link(path,content,type,ptime,hotlink)values('"+pathname+"','"+name+"','1','"+ptime+"','"+link+"')";
 
int ac=data.executeOperate(sql);
 
if(ac==1)
 
{
 
out.println("<script>alert('发布成功!');</script>");
 
out.println("<script>window.location='uplink.jsp';</script>");
 
}
 
%>
 
</body>
 
</html:html>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!