1、jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>上传图片</title>
<script type="text/javascript">
//上传图片
function uploadPic()
{
//异步上传 jquery.form.js插件
//使用js模拟表单异步上传
var options = {
url : "/upload/uploadPic.do",
type : "POST",
dataType : "json",
success : function(data){
//data.url
if(undefined != data.url && null != data.url)
{
//将img标签的src属性值设为fastDfs服务器返回的url
$("#imgSrc").attr("src", data.url);
}
}
};
$("#jvForm").ajaxSubmit(options);
}
</script>
</head>
<body>
<div class="body-box" style="float:right">
<form id="jvForm" action="o_save.shtml" method="post">
<input type="hidden" name="id" value="${bran.id }"/>
<table cellspacing="1" cellpadding="2" width="100%" border="0" class="pn-ftable">
<tbody>
<tr>
<td width="20%" class="pn-flabel pn-flabel-h">
<span class="pn-frequired">*</span>
上传图片(90x150尺寸):</td>
<td width="80%" class="pn-fcontent">
注:该尺寸图片必须为90x150。
</td>
</tr>
<tr>
<td width="20%" class="pn-flabel pn-flabel-h"></td>
<td width="80%" class="pn-fcontent">
<img width="100" height="100" id="imgSrc"/>
<input type="file" name="pic" onchange="uploadPic()"/>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>

2、Controller

import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;import cn.itcast.core.service.product.UploadService; @Controller
public class UploadController
{
@Autowired
private UploadService uploadService; //上传单张图片
@RequestMapping(value = "/upload/uploadPic.do")
public void toEdit(@RequestParam(required = false) MultipartFile pic,
HttpServletResponse response)
{
System.out.println(pic.getOriginalFilename());
try
{
String name = pic.getOriginalFilename(); //文件名
long size = pic.getSize(); //文件大小 // group1/M00/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg
String path = uploadService.uploadPic(pic.getBytes(), name, size);
String imageUrl = "http://192.168.172.20/"; //tracker的服务器的URL,注意后面需要加/
       String url = imageUrl + path; JSONObject json = new JSONObject(); //注意包路径
json.put("url", url);
json.put("path", path); //json
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(json.toString()); //返回数据
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

3、Service文件

import org.springframework.stereotype.Service;
import cn.itcast.common.fdfs.FastDfsUtils; @Service("uploadService")
public class UploadServiceImpl implements UploadService
{
@Override
public String uploadPic(byte[] pic, String name, long size) throws Exception
{
return FastDfsUtils.uploadPic(pic, name, size);
}
}

4、fastdfs的工具类

import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource; /*
* 连接fastdfs, 上传图片
* 返回上传的路径, 例如: group1/M00/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg
*/
public class FastDfsUtils
{
//上传图片
public static String uploadPic(byte[] pic, String name, long size) throws Exception
{
//读取配置文件
ClassPathResource resource = new ClassPathResource("fdfs_client.conf"); //从classpath路径下读取文件
ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath()); //连接tracker的客户端
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); //连接storage的客户端
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer); //上传图片
String ext = FilenameUtils.getExtension(name); //获取扩展名
NameValuePair[] metaArr = new NameValuePair[3]; //描述信息
metaArr[0] = new NameValuePair("fileName", name);
metaArr[1] = new NameValuePair("fileExt", ext);
metaArr[2] = new NameValuePair("fileSize", String.valueOf(size)); String path = storageClient1.upload_file1(pic, ext, metaArr); //返回存储的文件路径
return path;
}
}

如下是ajax请求的响应消息:

fastdfs 上传图片 完整版的更多相关文章

  1. C#.Net 上传图片,限制图片大小,检查类型完整版

    C#.Net 上传图片,限制图片大小,检查类型完整版 源代码: 处理图片类,如检查图片大小,按宽度比例缩小图片 public class CImageLibrary{   public enum Va ...

  2. 如何安全的将VMware vCenter Server使用的SQL Server Express数据库平滑升级到完整版

    背景: 由于建设初期使用的vSphere vCenter for Windows版,其中安装自动化过程中会使用SQL Server Express的免费版数据库进行基础环境构建.而此时随着业务量的增加 ...

  3. Android版的菜谱客户端应用源码完整版

    Android版的菜谱客户端应用源码完整版,这个文章是从安卓教程网转载过来的,不是本人的原创,希望能够帮到大家的学习吧. <ignore_js_op> 152936qc7jdnv6vo0c ...

  4. sed实例精解--例说sed完整版

    原文地址:sed实例精解--例说sed完整版 作者:xiaozhenggang 最近在学习shell,怕学了后面忘了前面的就把学习和实验的过程记录下来了.这里是关于sed的,前面有三四篇分开的,现在都 ...

  5. flexbox-CSS3弹性盒模型flexbox完整版教程

    原文链接:http://caibaojian.com/flexbox-guide.html flexbox-CSS3弹性盒模型flexbox完整版教程 A-A+ 前端博客•2014-05-08•前端开 ...

  6. 转贴 IT外企那点儿事完整版

    转贴 IT外企那点儿事完整版 第一章:外企也就那么回儿事(http://www.cnblogs.com/forfuture1978/archive/2010/04/30/1725341.html) 1 ...

  7. office2016 软件全集 官方下载免费完整版(含破解文件)不含垃圾软件 win10完美激活

    office2016官方下载免费完整版是新一代办公软件,office2016官方下载免费完整版已经分享到下面,office2016官方下载免费完整版包括了Word.Excel.PowerPoint.O ...

  8. 老王Python培训视频教程(价值500元)【基础进阶项目篇 – 完整版】

    老王Python培训视频教程(价值500元)[基础进阶项目篇 – 完整版] 教学大纲python基础篇1-25课时1.虚拟机安装ubuntu开发环境,第一个程序:hello python! (配置开发 ...

  9. thinkPHP3.2.3完整版 在sae上面的部署

    第一步:         thinkPHP3.2.3完整版,目录结构如下 第二步:在新浪sae上面创建一个新应用 第三步:用svn  down,下来会有两个文件:index.php.config.ya ...

随机推荐

  1. quartz 实例

    第一步:添加jar包 第二步:在spring配置文件中添加 <context:annotation-config/> 第三步:编写定时代码 我们通常做Java后台接口,是让前端访问的,让前 ...

  2. 微信小程序-获取当前城市位置及再次授权地理位置

    微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...

  3. 移动端web页面滚动不流畅,卡顿闪烁解决方案

    移动端web页面滚动不流畅,卡顿闪烁解决方案   1.ios端的-webkit-overflow-scrolling属性可控制页面滚动效果,设置如下实现惯性滚动和弹性效果: -webkit-overf ...

  4. js异步加载的5种方式

    方案1:$(document).ready 点评: 1.需要引用jquery 2.兼容所有浏览器. 方案2:<script>标签的async="async"属性 asy ...

  5. 2018面向对象程序设计(Java)第12周学习指导及要求

    2018面向对象程序设计(Java)第12周学习指导及要求 (2018.11.15-2018.11.18)   学习目标 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: (2) 掌 ...

  6. Codeforces Round #499 (Div. 2) C Fly题解

    题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...

  7. java中map接口hashMap以及Enty之间的用法和关系

    java中map接口hashMap以及Enty之间的转换 首先说的是map接口: Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value ...

  8. ncat的使用

    由于netcat的缺陷,所以有了升级版ncat,弥补了netcat的一些不足. ncat是nmap工具包的一个工具. 服务器端 ncat -c bash --allow 192.168.1.119 - ...

  9. Oracle中dbms_random.string 的用法

    转载:https://blog.csdn.net/simonchi/article/details/8657787 DBMS_RANDOM.STRING(var1,var2) 这个函数有两个参数 va ...

  10. 虚拟机安装的ubutun全屏

    虚拟机下面安装了ubuntu系统,显示的屏幕只有那么一小块儿,不知道如何才能全屏,那么如何全屏呢?且看下面经验.   方法/步骤     打开虚拟机,并点击要更改成全屏的那个ubuntu系统的电源 我 ...