展开全部
在jsp页面中你需要导入jsmart的中文jar包,如果你需要的话可以密我,我邮箱发给你
上传页面upload.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="uploadfile.jsp" method="post" enctype="multipart/form-data">
<table width="380" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size:12px;">
<tr>
<td height="40" align="right">节目名称:</td>
<td><input type="text" style="WIDTH: 210px" id="mcdb_content" name="mcdb_content" /></td>
</tr>
<td height="40" align="right">节目链接地址:</td>
<td><input type="text" style="WIDTH: 210px" id="linkUrl" name="linkUrl" /></td>
</tr>
<tr>
<td height="40" align="right" valign="middle">
上传焦点图片:</td>
<td><input type="file" name="upfile" id="file"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
处理上传文件的页面uploadfile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*;" %>
<%
SmartUpload mySmartUpload =new SmartUpload();
//上传文件的限制大小
long file_size_max=4000000;
String fileName2="",ext="",testvar="";
//应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
String url="/upload/";
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
mySmartUpload.setAllowedFilesList("bmp,jpg,tiff,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw");//此处的文件格式可以根据需要自己修改
//上载文件
mySmartUpload.upload();
} catch (Exception e){
//上传类型出现异常
%>
<SCRIPT language=javascript>
alert("只允许上传图片");
window.location='upload.html';
</script>
<%
}
try{
//得到上传的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//如果文件获得为空
if (myFile.isMissing()){%>
<SCRIPT language=javascript>
alert("请先选择要上传的文件");
window.location='upload.html';
</script>
<%}
//得到了上传的文件
else{
//String myFileName=myFile.getFileName(); //取得上载的文件的文件名
ext= myFile.getFileExt(); //取得后缀名
int file_size=myFile.getSize(); //取得文件的大小
String saveurl="";
//如果文件没有超过限制上传的大小
if(file_size<file_size_max){
//更改文件名,取得当前上传时间的毫秒数值
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar.getTimeInMillis());
saveurl=application.getRealPath("/")+url;
String filenamea="";
//保存路径
saveurl+=filename+"."+ext;
filenamea+=filename+"."+ext;
//接受普通参数
Request req = mySmartUpload.getRequest();
String bt=(String)req.getParameter("mcdb_content");
String linkurl = (String)req.getParameter("linkUrl");
myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
out.print(filenamea);
}
}
}catch (Exception e){
out.print(e.toString());
}
%>
上传页面upload.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="uploadfile.jsp" method="post" enctype="multipart/form-data">
<table width="380" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size:12px;">
<tr>
<td height="40" align="right">节目名称:</td>
<td><input type="text" style="WIDTH: 210px" id="mcdb_content" name="mcdb_content" /></td>
</tr>
<td height="40" align="right">节目链接地址:</td>
<td><input type="text" style="WIDTH: 210px" id="linkUrl" name="linkUrl" /></td>
</tr>
<tr>
<td height="40" align="right" valign="middle">
上传焦点图片:</td>
<td><input type="file" name="upfile" id="file"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
处理上传文件的页面uploadfile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*;" %>
<%
SmartUpload mySmartUpload =new SmartUpload();
//上传文件的限制大小
long file_size_max=4000000;
String fileName2="",ext="",testvar="";
//应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
String url="/upload/";
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
mySmartUpload.setAllowedFilesList("bmp,jpg,tiff,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw");//此处的文件格式可以根据需要自己修改
//上载文件
mySmartUpload.upload();
} catch (Exception e){
//上传类型出现异常
%>
<SCRIPT language=javascript>
alert("只允许上传图片");
window.location='upload.html';
</script>
<%
}
try{
//得到上传的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//如果文件获得为空
if (myFile.isMissing()){%>
<SCRIPT language=javascript>
alert("请先选择要上传的文件");
window.location='upload.html';
</script>
<%}
//得到了上传的文件
else{
//String myFileName=myFile.getFileName(); //取得上载的文件的文件名
ext= myFile.getFileExt(); //取得后缀名
int file_size=myFile.getSize(); //取得文件的大小
String saveurl="";
//如果文件没有超过限制上传的大小
if(file_size<file_size_max){
//更改文件名,取得当前上传时间的毫秒数值
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar.getTimeInMillis());
saveurl=application.getRealPath("/")+url;
String filenamea="";
//保存路径
saveurl+=filename+"."+ext;
filenamea+=filename+"."+ext;
//接受普通参数
Request req = mySmartUpload.getRequest();
String bt=(String)req.getParameter("mcdb_content");
String linkurl = (String)req.getParameter("linkUrl");
myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
out.print(filenamea);
}
}
}catch (Exception e){
out.print(e.toString());
}
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询