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

前端代码:

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

然后这是

<div data-options="prompt:'图片展示'" id="img" style="text-algin:center;height:150px;width: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; }

实体层不写了

ftp实现图片上传,文件也类似的更多相关文章

  1. FTP主动模式上传文件时返回"ftp: accept: Resource temporarily unavailable"

    FTP主动模式上传文件时返回 Passive mode off ftp: accept: Resource temporarily unavailable 这个问题要从ftp的2种模式说起 PORT ...

  2. FTP与HTTP上传文件的对比

    许多站点,比如facebook或一些博客等都允许用户上传或下载文件,比如论坛或博客系统的图片. 在这种情况下,通常有两种选择上传文件到服务器,那就是FTP协议和HTTP协议. 以下列出了一些两者的不同 ...

  3. ftp配置 Laravel上传文件到ftp服务器

    listen=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask= dirmessage_enable=YES ...

  4. java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器

    首先需要用到jsch-0.1.54.jar 包: 链接: https://pan.baidu.com/s/1kZR6MqwpCYht9Pp_D6NKQw 密码: gywx 直接上代码: package ...

  5. php 图片上传 文件上传 大小 限制

    nginx  413 Request Entity Too Large Php无法上传文件 查看php脚本运行用户,写个php脚本 <?php echo shell_exec("id ...

  6. Ftp客户端(上传文件)

    #coding=utf-8 import os import socket import hashlib import json # client = socket.socket() #申明socke ...

  7. FTP FtpWebRequest 异步上传文件

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. ftp服务器不能上传文件故障

    1.在客户端lftp命令无法put文件 原因:登陆用户无法读写 ftp服务器的文件夹,在服务器上增加权限  chmod 777  即可 还有一种方法:在 vsftp的配置文件里,设置可匿名读写

  9. ftp免交互上传文件脚本

    ftp -i -n <<! open .x.x.x user yourFtpAccount yourPasswd cd /root/DailyBuild/webapps/ delete x ...

随机推荐

  1. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  2. 动态限制EdiText仅仅能输入特定字符

    怎样设置EditText,使得仅仅能输入数字或者某些字母呢? 一.设置EditText,仅仅输入数字: 方法1:直接生成DigitsKeyListener对象就能够了. et_1.setKeyList ...

  3. openVswitch(OVS)源码分析之工作流程(哈希桶结构体的解释)

    这篇blog是专门解决前篇openVswitch(OVS)源码分析之工作流程(哈希桶结构体的疑惑)中提到的哈希桶结构flex_array结构体成员变量含义的问题. 引用下前篇blog中分析讨论得到的f ...

  4. HDU 5889 Barricade (Dijkstra+Dinic)

    思路: 首先 先Dijkstra一遍 找出来最短路 不是最短路上的边都不要 然后呢 套个Dinic模板就好了-- 求个最小割 输出 大功告成~~ //By SiriusRen #include < ...

  5. 快速select算法的实现

    代码来自: http://blog.csdn.net/v_JULY_v 算法思想: // Quick_select.cpp : 定义控制台应用程序的入口点. // #include "std ...

  6. HDU 4372 Count the Buildings

    Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. 批处理实现添加java环境变量

    作者:朱金灿 来源:http://blog.csdn.net/clever101 从网上搜了一些资料,再修改测试,终于通过了win7系统的测试.代码如下: @echo off rem 本批处理文件目的 ...

  8. 编写jsp代码时出现的红色提示线错误

    将jsp页面关闭:点击"X"号,例如图中的index.jsp页面的”X"号,或者右键—“close"也可以.   双击jsp页面:重新启动页面,页面的错误提示线 ...

  9. GHO文件内IE主页的修改方法

    修改方法: 1.先打开映像  GHOSTexp 打开GHO文件 2.提取注册表文件  C:\WINDOWS\SYSTEM32\CONFIG 下就是系统的注册表文件,详细见下 3.打开本地的注册表,加载 ...

  10. 【福利】微信小程序130个精选Demo合集

    小编最近在开发小程序,也读到了不少优秀的小程序源码,项目中有些需求可以直接从源码里粘贴复制过来,虽然这样做不利于自己独立编写代码,但比较是给公司做项目啊,秉着效率第一的原则,简直没有什么比ctrl+c ...