本来用得是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. Android布局文件的载入过程分析:Activity.setContentView()源代码分析

    大家都知道在Activity的onCreate()中调用Activity.setContent()方法能够载入布局文件以设置该Activity的显示界面.本文将从setContentView()的源代 ...

  2. 会变得ActionBar,让你的ActionBar与众不同

    话不多说先看两张图: github地址:https://github.com/Smalinuxer/android-SlideActionBar 原理什么的有时间再讲,或者自行看代码; 兴许还会补充新 ...

  3. Android开发:怎样把Android studio中的Library公布到Jcenter

    本人之前写了个简单的库,想放到Jcenter上.查过各种资料.踩过各种坑,久经折腾.最终发现了一个很easy而且高效的方法.现分享出来,该方法本人亲測可用,实现起来大概仅仅须要半个小时.这种方法是国外 ...

  4. Linux常用的安全工具

    Linux常用的安全工具 "工欲善其事,必先利其器".作为一个合格的系统管理员,要应对可能发生的安全事件,掌握Linux下各种必须的安全工具是首要大事.本文主要介绍Linux上常用 ...

  5. web.config添加identity impersonate="true"导致拒绝访问

    例:<identity impersonate="tr" userName="AD\name" password="word"/> ...

  6. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  7. 紫书 习题 10-21 UVa 1649 (组合数)

    C(n, k) = m, 固定k,枚举k 这里用到了组合数的一个性质 当k固定的时候,C(2 * k, k) 最小 C(m, k)最大(对于这道题而言是这样,因为大于m 就最终答案不可能为m了) 所以 ...

  8. CCF模拟题 最优配餐

    最优配餐 时间限制: 1.0s 内存限制: 256.0MB   问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题. 栋栋的连锁店所在 ...

  9. Maintaining processor resources during architectural events

    In one embodiment of the present invention, a method includes switching between a first address spac ...

  10. NStimer 被堵塞

    我们在界面上滚动一个scrollview,那么我们会发如今停止滚动前,会发现NSTimer未被运行.就好像scrollView在滚动的时候将timer暂停了一样,在查看对应文档后发现,这事实上就是ru ...