zoukankan      html  css  js  c++  java
  • ftp实现图片上传,文件也类似

    本来用得是easyui得控件 点击按钮实现选择图片得 ,但是老板非得要双击图片框实现上传代码。。。。做个简单得记录

    前端代码:

    首先,<form>表单要加上 enctype="multipart/form-data"  才能实现上传文件

    然后这是

    <div data-options="prompt:'图片展示'" id="img" style="text-algin:center;height:150px;205px;vertical-align:middle;border:1px solid #95B8E7;border-radius:5px" >
        <img id="urlImg" width="205px" height="150px" ondblclick="fileSelect();">
    </div>

    下面是双击事件,  下面得是图片预览 ,上篇随笔介绍过了

    function fileSelect() {
                document.getElementById("fileToUpload").click(); 
            }
            
            $("#fileToUpload").change(function(){
                $("#urlImg").attr("src",URL.createObjectURL($(this)[0].files[0]));
            });

    接下来的后端代码

    数据库加了个url的字段,用来存储图片名称,名称是随机生成的

    Action层:

    //添加
        public void insertSpecificMCodeInformation() {
                
                SpecificMCodeInformation specificMCodeInformation = new SpecificMCodeInformation();
                
                //mCodeInformation.setTypeID(parentID+typeID);
                //specificMCodeInformation.setTypeID(typeID);
                specificMCodeInformation.setParentID(parentID);
                specificMCodeInformation.setIllustration(illustration);
                specificMCodeInformation.setTypeName(typeName);
                specificMCodeInformation.setEntryUnit(entryUnit);
                specificMCodeInformation.setAlias(alias);
                specificMCodeInformation.setAlias1(alias1);
                specificMCodeInformation.setAlias2(alias2);
                specificMCodeInformation.setEntryPerson(entryPerson);
                specificMCodeInformation.setEntryDate(DateStringUtil.DateToStr(new Date()));
                specificMCodeInformation.setNorms(norms);
                specificMCodeInformation.setModel(model);
                specificMCodeInformation.setUrl(url);
                if( fileToUpload != null ) {
                    // 统一调用一个上传方法,方便后期维护
                    String img = specificMCodeInformationService.saveContImg(fileToUpload, fileToUploadFileName, typeID);
                    specificMCodeInformation.setUrl(img);
                    System.out.println("IMGGGGGGGGGGGGGGGGG"+specificMCodeInformation.getUrl());
                }
                
                specificMCodeInformationService.insertSpecificMCodeInformation(specificMCodeInformation);
                writeJson(specificMCodeInformation);
            }
        

    DAO层:

        public int insertSpecificMCodeInformation(@Param("specificMCodeInformation")SpecificMCodeInformation specificMCodeInformation);

    Service层:

        public void insertSpecificMCodeInformation(SpecificMCodeInformation specificMCodeInformation);
        public String saveContImg( File upload, String uploadFileName, String typeID );

    实现层:

    @Override
        public void insertSpecificMCodeInformation(SpecificMCodeInformation specificMCodeInformation) {
            // TODO Auto-generated method stub
            try {
                System.out.println(createTypeID(specificMCodeInformation.getParentID()));
                System.out.println("alias:"+specificMCodeInformation.getAlias());
                System.out.println("norms:"+specificMCodeInformation.getNorms());
                System.out.println("model:"+specificMCodeInformation.getModel());
                System.out.println("entryUnit:"+specificMCodeInformation.getEntryUnit());
                specificMCodeInformation.setTypeID(createTypeID(specificMCodeInformation.getParentID()));
                System.out.println("typeID"+specificMCodeInformation.getTypeID());
                System.out.println("url"+specificMCodeInformation.getUrl());
                specificInformationMapper.insertSpecificMCodeInformation(specificMCodeInformation);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            
        }

    这个用来生成文件名:

    @Override
        public String saveContImg(File fileToUpload, String fileToUploadFileName, String typeID) {
            String saveFileName = "";
            FTPUtil f = new FTPUtil();
            try {
                String ip = ReadProperty.getValue( "common.properties", "ftp.url" );
                String username = ReadProperty.getValue( "common.properties", "ftp.username" );
                String password = ReadProperty.getValue( "common.properties", "ftp.password" );
                String port = ReadProperty.getValue( "common.properties", "ftp.port" );
                int iPort = Integer.valueOf( port );
                f.initClient( ip, iPort, username, password );
                InputStream in = new FileInputStream( fileToUpload );
                System.out.println("INNNN"+in);
                String path = ReadProperty.getValue( "common.properties", "ftp.materiel" ) + typeID + "\";
                System.out.println("PATHHHHH"+path);
                saveFileName = GUID.getGUID() + fileToUploadFileName.substring( fileToUploadFileName.lastIndexOf( "." ) );
                System.out.println("FILENameEEEEE"+saveFileName);
                f.upLoadFile( path, saveFileName, in );
            } catch( Exception e ) {
                e.printStackTrace();
            } finally {
                f.closeConnection();
            }
            return saveFileName;
        
        }

    实体层不写了

  • 相关阅读:
    fatal error C1071: unexpected end of file found in comment
    error: reference to 'max' is ambiguous
    STL list链表的用法详解
    头文件
    error C4430: missing type specifier
    python selenium-2 定位元素
    selenium page object模式
    selenium进阶
    导入testng管理测试用例
    selenium java-3 定位元素的八种方法
  • 原文地址:https://www.cnblogs.com/liaoxun/p/10316937.html
Copyright © 2011-2022 走看看