struts1图片上传

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)


PictureHandleTool.uploadPricture(imageVo, form,ModifyFlag);

===========================


/**

 * 图片的管理方法
 * @author Administrator
 *
 */
public class PictureHandleTool {
     public final static String PIECTURE_SAVE_PATH = "/home/caissa/upload/";//图片的存储路径
     /**
      * 图片删除
      * @param imageName
      */
    public static void deletePicture(String imageName){
        File file=new File(PIECTURE_SAVE_PATH+imageName);
        if(file.exists()){
            file.delete();
        }
    }
    

    /**
     * 上传文件方法
     * @param imageVo
     * @param form
     * @return
     * @throws IOException
     */
    public static int uploadPricture(DictImageVo imageVo, ActionForm form,String ModifyFlag)
            throws IOException {
        PictureUploadForm uploadForm = (PictureUploadForm) form;
        FormFile file = uploadForm.getFileGuidPic();
        int fileSize = file.getFileSize();
        int fileSizeKB = fileSize / 1024;// KB
        if (fileSizeKB < 800) {// 判断大小,大于800KB直接返回
            if("1".equals(ModifyFlag)){
                deletePicture(imageVo.getImagePath());
            }
            String fileName = file.getFileName();
            InputStream is = null;
            OutputStream os = null;
            try {
                is = file.getInputStream();
                File savePath=new File(PIECTURE_SAVE_PATH);
                if (!savePath.exists()) {// 没有这个目录则创建
                    savePath.mkdir();
                }
                int beginIndex = fileName.lastIndexOf(".");
                Date date = new Date(); // 文件名
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSSSSSSS");
                String newFileName = String.valueOf(sdf.format(date)+ fileName.substring(beginIndex, fileName.length()));
                String imagePath = savePath.getPath() + "/" + newFileName;// 保存图片路径
                imageVo.setImagePath(newFileName);
                os = new FileOutputStream(imagePath);
                byte[] bytes = new byte[4 * 1024];
                while ((is.read(bytes)) != -1) {
                    os.write(bytes);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                is.close();
                os.close();
            }
        }
        return fileSizeKB;

    }


=========================ActionForm==========================

public class PictureUploadForm extends ActionForm{
    private FormFile fileGuidPic;

    public FormFile getFileGuidPic() {
        return fileGuidPic;
    }

    public void setFileGuidPic(FormFile fileGuidPic) {
        this.fileGuidPic = fileGuidPic;
    }
    
}


=============================jsp

放大图片

function showPic(imagePath){   //查看图片
        var x,y;  x = document.body.scrollWidth/2-400;  y = 0;
        document.getElementById("showpic").style.left = x;
        document.getElementById("showpic").style.top = y;
        document.getElementById("showpic").innerHTML = "<img width=500px height=450px src=\"" + imagePath + "\"  οnclick=hiddenPic();>";
        document.getElementById("showpic").style.display = "block";
    }
    function hiddenPic(){  
        document.getElementById("showpic").innerHTML = "";  document.getElementById("showpic").style.display = "none";
    }

------------------------------------------------------------------------------------------

 function setImagePreview() {//用于显示图片
        
            if(valUpdatePhoto()==false){
                return false;
            }
            var docObj=document.getElementById("fileGuidPic");
            var imgObjPreview=document.getElementById("preview");
                        docObj.select();
                        var imgSrc = document.selection.createRange().text;
                        var localImagId = document.getElementById("localImag");
                        //必须设置初始大小
                        localImagId.style.width = "120px";
                        localImagId.style.height = "100px";
                        //图片异常的捕捉,防止用户修改后缀来伪造图片
                        try{
                                localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                                localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
                        }catch(e){
                                return false;
                        }
                        imgObjPreview.style.display = 'none';
                        document.selection.empty();
                return true;
        }

配置文件-=====================================


struts配置文件-------------------


<form-beans>
        <form-bean name="uploadForm" type="cn.com.caissa.erp.rm.action.PictureUploadForm"></form-bean>
    </form-beans>
    <action-mappings>
        <action name="uploadForm" type="cn.com.caissa.erp.rm.action.PictureUploadAction" scope="request" path="/PictureUploadAction" parameter="cmd">

在 jboss中--------------------<IMG alt="" src="/upload/图片名.jpg"/>

< Context  path = "/upload"  docBase = "/home/upload/"  debug = "1"  reloadable = "true"  override = "true"  crossContext = "true" />把这句加在host中间,用于找图片路径


这个只能显示数字或英文的图片名,要想显示中文的图片名,则要在server.xml
<Connector port="8066" address="${jboss.bind.address}"
         maxThreads="250" strategy="ms" maxHttpHeaderSize="8192"
         emptySessionPath="true"
         enableLookups="false" redirectPort="8443" acceptCount="100"
         connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/>
在你的配置文件中加入URIEncoding="UTF-8"  指定url为uft-8,
如果src 是接参数,




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值