upload2.jsp
<form action="upload3.jsp" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
上传文件:<input type="file" name="file" size="20"></td></tr>
<tr> <td>
上传图片:<input type="file" name="file" size="20"></td></tr>
<tr>
<td>
<input type="submit" value="上传">
</td>
</tr>
</table>
</form>
upload3.jsp
try{
String str_path = pageContext.getServletContext().getRealPath("/")+"WebRoot//temp//";
mySmartUpload.initialize(pageContext);
mySmartUpload.setMaxFileSize(1024*1024*1024);
//开始上传
mySmartUpload.upload();
File file=mySmartUpload.getFiles().getFile(0);
File file2=mySmartUpload.getFiles().getFile(1);
System.out.println("size1 == "+file.getSize());
System.out.println("size2 == "+file2.getSize());
String fn=file.getFieldName();
com.smsgov2.dy.sql.MyDataBaseManager_2 mdbm = new com.smsgov2.dy.sql.MyDataBaseManager_2();
mdbm.open();
double size = (double)file.getSize();
System.out.println("file1 size"+file.getSize());
System.out.println("file2 size"+file2.getSize());
String str = "";
if(size < 1024*1024){
str = Math.floor(size/1024*10)/10 + "KB";
}else{
str = Math.floor(size/1024/1024*10)/10 + "MB";
}
String sql = "update down_table set file_size = '"+str+"' ,";
System.out.println("size = ==============="+str);
String date = new java.text.SimpleDateFormat("HH-mm-ss").format(new java.util.Date());
//取得文件的扩展名
if(!file.isMissing()){
String ext=file.getFileExt();
String filepath = str_path+date+file.getFileName();
//保存的绝对路径
if(filepath!=null){
if(filepath.indexOf("")==-1){
String strtemp ="";
for(int i=0; i<filepath.length();i++){
if(filepath.charAt(i)=='//'){
strtemp+="";
}else{
strtemp+=filepath.charAt(i);
}
}
filepath = strtemp;
}}
file.saveAs(filepath);
System.out.println(filepath);
sql += " down_url = '"+filepath+"', down_name = '"+date+file.getFileName()+"' ,";
}
if(!file2.isMissing()){
String ext=file2.getFileExt();
//保存的绝对路径
String filepath = str_path+date+file2.getFileName();
if(filepath!=null){
if(filepath.indexOf("")==-1){
String strtemp ="";
for(int i=0; i<filepath.length();i++){
if(filepath.charAt(i)=='//'){
strtemp+="";
}else{
strtemp+=filepath.charAt(i);
}
}
filepath = strtemp;
}}
file2.saveAs(filepath);
sql += " pic_url = '"+filepath+"' ,pic_name = '"+date+file2.getFileName()+"'";
}
sql += " where update_t = '" + session.getAttribute("date").toString()+"'";
System.out.println(sql);
mdbm.executeUpdate(sql);
mdbm.close();
out.println("<script>alert('上传成功');</script>");
jspsmartupload简单用法以及将路径存入数据库
最新推荐文章于 2021-01-28 20:04:36 发布