<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="java.util.Calendar,java.io.File"%>
<%
request.setCharacterEncoding("gb2312");
try{
SmartUpload su = new SmartUpload(); /// 上傳文件組件
long file_max_size = 4000000;
su.initialize(pageContext);// 上傳初始化
su.upload();
// 將上傳文件全部保存到指定目錄
String temp="upload/images";
//創建文件夾使用絕對路徑
String uploadPath =request.getRealPath("/")+temp;
//如果文件夾不存在,則創建此文件夾
if(!new File(uploadPath).isDirectory()){
new File(uploadPath).mkdirs();
}
Files files=su.getFiles();
//得到上傳的文件
com.jspsmart.upload.File file = su.getFiles().getFile(0);
if(file.isMissing()){
System.out.println("missing");
}
String postfix="."+file.getFileExt();
//為上傳的文件起別名
System.out.println("後綴名是:" + postfix);
if(!postfix.equals(".jpg")){
%>
<script type="text/javascript">
alert("不支持此圖片格式");
</script>
<%
}else{
String fileName = "test";
//這個就是可以得到圖片的路徑了
String strtemp=uploadPath+"/"+ fileName + postfix;
System.out.println(strtemp);
file.saveAs(strtemp); ///參數是路徑 (完整)
%>
<script>
alert("圖片上傳成功!");
</script>
<%
}
}catch (Exception e){
System.out.println(e.getMessage());
%>
<script type="text/javascript">
alert("圖片上傳失敗!");
</script>
<%
}
%>
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>首頁</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="dscription" content="This is my page">
</head>
<body>
<form action="ImageUpload.jsp" method="post" enctype="multipart/form-data">
<table>www.2cto.com
<tr>
<td width="120" align="center">路徑:</td>
<td width="180"><input type="file" name="file" class="from_len21" contenteditable="false"></td>
</tr>
<tr>
<td>
<input type="submit" value="確定" />
</td>
</tr>
</table>
</form>
</body>
</html>