Js上传图片并显示缩略图的流程为

Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图

本文上传图片所用的Js库是ajaxfileupload,下载地址:http://files.cnblogs.com/files/yujie365/ajaxfileupload.js,Html布局采用bootstrap.

后端处理流程使用Java中的Spring框架进行处理

前面页面处理流程

1,Html页面定义File:

2,选择图片并上传:

*绑定fileimage控件的change事件:$("#fileimage").bind("change", uploadFileImage);

*上传图片:

function uploadFileImage() {

var objUrl = getObjectURL(this.files[0]);

// 上传图片

uploadFile("uploadimage/fileimage", "fileimage", function(result) {

});
} //uploadUrl:服务器用于接收图片的接口
//fileElementID:file控件的id
//callback:图片上传完成后的回调方法。
function uploadFile(uploadUrl, fileElementID, callback) {
$.ajaxFileUpload({
url : uploadUrl,
secureuri : false,
type : 'post',
fileElementId : fileElementID,
dataType : 'JSON',
success : function(data) {
//data:服务器返回的结果
if (data&& callback != null) {
callback(data);
}
},
error : function(data, status, e) { }
});
}

*显示缩略图:

更新上面uploadFileImage方法为下面代码:

function uploadFileImage() {

var objUrl = getObjectURL(this.files[0]);

// 上传图片

uploadFile("uploadimage/fileimage", "fileimage", function(result) {

// 显示缩略图

console.log("objUrl = " + objUrl);

if (objUrl) {

$("#fileimage_thumbnail").attr("src", objUrl);

}

});

}

后台Java处理流程

1,后台接收图片的方法如下:

@RequestMapping(value = "uploadimage/fileimage")

public void uploadPicture(HttpServletRequest request) {

	try {
String filePath = uploadFile(request, pictureUploadService);
if (!Tools.isEmptyString(filePath)) {
setResult(filePath);
logHelper.info("图片上传成功...");
} else {
setResult(
MessageManager.getInstance().getMessage(
"pictureuploadfailed"),
ResultType.Error);
logHelper.info("图片上传失败...");
}
} catch (IllegalStateException e) {
logHelper.warn("图片保存失败:" + e.getMessage());
} catch (IOException e) {
logHelper.warn("图片保存失败:" + e.getMessage());
}
}

2,存储图片处理流程

private String uploadFile(HttpServletRequest request,IUploadFile uploadFile) throws IllegalStateException,

IOException {

String filePath="";

	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext()); if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; Iterator<String> fileNames = multipartHttpServletRequest
.getFileNames(); while (fileNames.hasNext()) {
MultipartFile file = multipartHttpServletRequest
.getFile(fileNames.next().toString()); if (file != null) {
filePath = uploadFile.uploadFile(file);
if (!Tools.isEmptyString(filePath)) {
setResult(filePath);
} else {
setResult(
MessageManager.getInstance().getMessage(
"pictureuploadfailed"),
ResultType.Error);
}
}
}
} return filePath;
}

public interface IUploadFile {

public String uploadFile(IMultipartFile multipartFile) throws IllegalStateException, IOException ;

}

@Service

public class PictureUploadService extends BaseService implements IUploadFile {

/**
* 上传图片
* @param multipartFile
* 用户所上传的图片文件
* @return
* @throws IllegalStateException
* @throws IOException
*/
@Override
public String uploadFile(MultipartFile multipartFile) throws IllegalStateException, IOException {
String relativePath = Helpers.getImageFolderPath(); String imageSavedPath = IOTools.transferFile(multipartFile,
ConfigHelper.IMAGE_SAVED_PATH, relativePath); logger.info("图片保存路径:" + imageSavedPath); return imageSavedPath;
}

}

package com.weijiazhe.utils;

import java.io.File;

import java.io.IOException;

import java.util.Dictionary;

import org.springframework.web.multipart.MultipartFile;

import com.weijiazhe.weijiazheutils.ConfigHelper;

public class IOTools {

/**
* 转移文件
*
* @param multipartFile
* @param fileAbsoluteSavedPath
* @param relativePath
* @return
* @throws IllegalStateException
* @throws IOException
*/
public static String transferFile(MultipartFile multipartFile,
String fileAbsoluteSavedPath, String relativePath)
throws IllegalStateException, IOException {
if (!Tools.isEmptyString(relativePath)) {
String fileName = Tools.getGuid();
String prefix = IOTools.getPrefix(multipartFile
.getOriginalFilename()); relativePath += "\\" + fileName + prefix; String fileSavedPath = fileAbsoluteSavedPath + relativePath; File file = new File(fileSavedPath); //文件不存在则创建文件.
if(!file.exists()){
//String dirName=file.getAbsolutePath();
//if(file.isDirectory()){
file.mkdirs();
//}
//fileSavedPath+="\\" + fileName + prefix;
//file=new File(fileAbsoluteSavedPath);
//file.createNewFile();
} multipartFile.transferTo(file);
} return relativePath;
}

}

Js上传图片并生成缩略图的更多相关文章

  1. C#上传图片和生成缩略图以及图片预览

    因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...

  2. PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图

    添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...

  3. MVC4 上传图片并生成缩略图

    Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...

  4. C#上传图片同时生成缩略图,控制图片上传大小。

    #region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...

  5. thinkphp上传图片,生成缩略图

    Image.php <?php /** * 实现图片上传,图片缩小, 增加水印 * 需要定义以下常量 * define('ERR_INVALID_IMAGE', 1); * define('ER ...

  6. C# webform上传图片并生成缩略图

    其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...

  7. MVC3.0 上传图片并生成缩略图

    转自:http://mikelai.blog.163.com/blog/static/18411126620118771732675/ Controller: public ActionResult ...

  8. 一例tornado框架下处理上传图片并生成缩略图的例子

    class coachpic(RequestHandler): @gen.coroutine def post(self): picurl = self.request.files[] print(& ...

  9. Asp.Net 上传图片并生成高清晰缩略图

    在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...

随机推荐

  1. 域名添加HTTPS

    准备 需要python版本为2.7以上,所以centos6需要把2.6升级成2.7 升级python ###安装python2.7 tar -xvf Python-2.7.5tar.bz2 cd Py ...

  2. vux picker

    1.通过实验证明: PopupPicker = TransferDom + Popup + PopupHeader + Picker 2.代码 Picker.vue <!-- Picker 组件 ...

  3. 怎样在OTN站点高速找到asm包并下载 (Oracle RAC)

    怎样在OTN站点高速找到asm包并下载 ***********************************************声明******************************* ...

  4. Highcharts构建加权平均值图表

    Highcharts构建加权平均值图表 加权平均值图表是将图表中多个数据列值.依据加权算法计算获取平均值,并加入生成一个加权折线.在这里,我们直接使用第三方插件Dynamic Weighted Ave ...

  5. 解决burp suite 使用chrome訪问https失真的问题

    用burp suite 訪问https网页 尤其使用chrome(有时候firefox也会) 会出现js或者css载入不出来的情况 这样的时候,导出burp suite的证书,保存为cer格式 然后进 ...

  6. 仰视源代码,实现strcmp

    //这是系统库的实现 int strcmp(const char* src, const char* dest) { int rtn = 0; while(!(rtn = *(unsigned cha ...

  7. 解决Hibernate4执行update操作,不更新数据的问题

    后台封装java对象,使用hibernate4再带的update,执行不更新数据,不报错. 下面贴出解决方法: 失败的方法 hibernate自带update代码:(失效) Session sessi ...

  8. redis Database Eviction Policies Redis on Flash

    Database Eviction Policies - Redis Enterprise Software | Redis Labs https://redislabs.com/redis-ente ...

  9. ugc pgc ogc web2.0 mgc

    http://yjy.people.com.cn/n/2014/0120/c245079-24169402.html machine

  10. bzoj 4337 树的同构

    4337: BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树 ...