JSP上传图片(或文件)并显示缩略图(附带解决IE7中无法显示缩略图)

本文介绍了一个基于JSP的图片上传系统实现方案,包括前端页面设计与后端处理逻辑。前端页面允许用户选择图片并实时预览,后端采用JspSmartUpload组件处理文件上传,展示了如何保存上传的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 第一个页面uploadImg.jsp
//上传使用第三方的包:JspSmartUpload,可以在csdn上下载到,需要的话可以联系我patriot074@163.com
<% @ page language = " java "   import = " java.util.* "  pageEncoding = " gb2312 " %>
<%
 request.setCharacterEncoding(
" gb2312 " );  //  这句话很重要,否则遇到中文就出错~
%>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html >
  
< head >
  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 "   />
    
< title > 测试文件上传页面 </ title >
< style type = " text/css " >
#newPreview
{
    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod
=scale);
}

</ style >
< script type = " text/javascript "  language = " javascript " >
var flag
= true ;
function PreviewImg(imgFile)
{
    
//新的预览代码,支持 IE6、IE7。
    var newPreview = document.getElementById("newPreview");
    newPreview.filters.item(
"DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
    
if(flag)
    
{
    newPreview.style.width 
= "240px";
    newPreview.style.height 
= "320px";
    }

}


function setImgWH()
{
    var newPreview 
= document.getElementById("newPreview");
    flag
=false;
    newPreview.style.width 
= document.getElementById("txtImgWidth").value;;
    newPreview.style.height 
= document.getElementById("txtImgHeight").value;
}

</ script >
  
</ head >
  
  
< body >
   
< form action = " upload_ok.jsp "  method = post name = " Upload "  enctype = " multipart/form-data " >
请输入附件文件的所在路径
< FONT color = red >   *   </ FONT > 为必填项目 < br >
        
< br >
        
< TABLE width = " 317 "  border = 0  cellPadding = 0 >
          
< TBODY >
            
< TR >  
              
< TD align = left vAlign = center nowrap > 附件路径: </ TD >
              
< TD >< input type = " file "  name = " file "  onchange = " javascript:PreviewImg(this); "  style = " border: 1px #FFFFFF solid;background:#efefef "   >   < FONT color = red >*</ FONT ></ TD >
              
< TD >< div id = " newPreview " ></ div ></ TD >
            
</ TR >
            
< TR >
            
< TD > 设置图片预览宽度: < br >< input type = " text "  id = " txtImgWidth " ></ TD >
            
< TD > 设置图片预览高度: < br >< input type = " text "  id = " txtImgHeight " ></ TD >
            
< TD >< BR >< input type = " button "  ID = " setImg "  value = " 设置 "  onclick = setImgWH() ></ TD >
            
</ TR >
            
< TR align = " left " >  
              
< TD height = 60  colspan = " 2 "  vAlign = center nowrap >   < INPUT style = " height:22px "  name = B1 type = submit value = "  确 定  "   >
< INPUT style = " height:22px "  name = B2 type = reset value = "  取 消  "   >  
              
</ TD >
            
</ TR >
          
</ TBODY >
        
</ TABLE >
        
< input type = " hidden "  id = " imgWidth "  value = " 240 " >
        
< input type = " hidden "  id = " imgHeight "  value = " 320 " >   

   
</ form >    
  
</ body >
</ html >

 

 

// 后台处理页面upload_ok.jsp
<% @ page contentType = " text/html; charset=gb2312 "  language = " java "  
import = " java.util.*,com.jspsmart.upload.* "  errorPage = ""   %>
< html >
<%  
    String   imgPath
= getServletContext().getRealPath( "" ) + " /Win/Img_CAD/ " ;
%>
< script type = " text/javascript " >
function countDown(secs)
{
if(secs>0)
setTimeout(
" ",secs*1000);
history.back(
1)
}

</ script >
< head >
< title > 文件上传处理页面 </ title >
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >
</ head >
< body >
<%
    
//  新建一个SmartUpload对象
    SmartUpload su  =   new  SmartUpload();
    
//  上传初始化
    su.initialize(pageContext);
    
//  设定上传限制
    
//  1.限制每个上传文件的最大长度。
    
//  su.setMaxFileSize(10000);
    
//  2.限制总上传数据的长度。
    
//  su.setTotalMaxFileSize(20000);
    
//  3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
    
//  su.setAllowedFilesList("doc,txt");
    
//  4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
    
// jsp,htm,html扩展名的文件和没有扩展名的文件。
    
//  su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
    
//  上传文件
    su.upload();
    
//  将上传文件全部保存到指定目录
    
// int count = su.save("imgPath");
    
// out.println(count+"个文件上传成功!<br>");
    
    
//  利用Request对象获取参数之值
    
    
//  逐一提取上传文件信息,同时可保存文件。
     for  ( int  i = 0 ;i < su.getFiles().getCount();i ++ )
    
{
        com.jspsmart.upload.File file 
= su.getFiles().getFile(i);
        
        
// 若文件不存在则继续
        if (file.isMissing()) 
        
{
        out.println(
"文件不存在");
        
%>
        
<script>
        countDown(
10);
        
</script>
        
<% 
        
continue;
        }

        
%>


<%
        
// 显示当前文件信息
        out.println("<TABLE BORDER=1>");
        out.println(
"<TR><TD>表单项名(FieldName)</TD><TD>"
        
+ file.getFieldName() + "</TD></TR>");
        out.println(
"<TR><TD>文件长度(Size)</TD><TD>" + 
        file.getSize() 
+ "</TD></TR>");
        out.println(
"<TR><TD>文件名(FileName)</TD><TD>" 
        
+ file.getFileName() + "</TD></TR>");
        out.println(
"<TR><TD>文件扩展名(FileExt)</TD><TD>" 
        
+ file.getFileExt() + "</TD></TR>");
        out.println(
"<TR><TD>文件全名(FilePathName)</TD><TD>"
        
+ file.getFilePathName() + "</TD></TR>");
        out.println(
"</TABLE><BR>");
        file.saveAs(imgPath
+su.getFiles().getFile(i).getFileName());
        out.println(imgPath);
        }

%>

</ body >
</ html >

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值