批量上传图片文件(copy 代码直接可用)

本文介绍了一个使用Spring框架实现的批量图片上传功能,包括文件处理、路径生成及异常捕获等关键步骤,适用于需要处理大量图片上传需求的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
/**
 * 批量上传图片文件
 * @author hxw
 *
 */
@Controller
public class sctppl {

    /**
     * @Description: 批量上传图片文件
     * @author: huxw
     * @date: 2018年11月23日
     * @return:String
     */
    @ResponseBody
    @RequestMapping("/uploadImgList.html")
    public String uploadPictureList(@RequestParam(value = "file", required = false) MultipartFile[] file,
            HttpServletRequest request) {
        System.out.println("上传");
        File targetFile = null;
        String msg = "";// 返回存储路径
        int code = 1;
        List imgList = new ArrayList();
        if (file != null && file.length > 0) {
            for (int i = 0; i < file.length; i++) {
                String fileName = file[i].getOriginalFilename();// 获取文件名加后缀
                if (fileName != null && fileName != "") {
/*
                    String returnUrl = request.getScheme() + "://" + request.getServerName() + ":"
                            + request.getServerPort() + request.getContextPath() + "/static/";// 存储路径
                                                                                                    // String
                    String path = request.getSession().getServletContext().getRealPath("static"); // 文件存储位置
*/
                    
                      String returnUrl = "E:/files/"; 
                      String path ="E:/files/";
                     
                    String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());// 文件后缀
                    fileName = new Date().getTime() + "_" + new Random().nextInt(1000) + fileF;// 新的文件名
                    Date currentTime = new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                    // 先判断文件是否存在
                    String fileAdd = formatter.format(currentTime);
                    //File file1 = new File(path + "/" + fileAdd);
                    File file1 = new File(path);
                    // 如果文件夹不存在则创建
                    if (!file1.exists() && !file1.isDirectory()) {
                        file1.mkdir();
                    }
                    targetFile = new File(file1, fileName);
                    try {
                        file[i].transferTo(targetFile);
                        msg = returnUrl + fileAdd + "/" + fileName;
                        imgList.add(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        // return JSON.toJSONString(imgList);
        return imgList.toString();
    }

}
 

在线演示:http://www.ncmem.com/products/image-uploader/demo/index.html 开发文档-ASP.NET(C#):http://www.cnblogs.com/xproer/archive/2011/01/09/1931278.html 开发文档-PHP:http://www.cnblogs.com/xproer/archive/2011/05/13/2045854.html 开发文档-JSP:http://www.cnblogs.com/xproer/archive/2011/05/20/2051887.html 产品介绍:http://www.cnblogs.com/xproer/archive/2010/08/09/1796077.html 升级日志:http://www.cnblogs.com/xproer/archive/2010/10/06/1844816.html 资源下载:crx安装包,xpi安装包,exe安装包,开发文档,ASP示例,ASP.NET示例,JSP示例,PHP示例, VC运行库:http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf 新颖网络图片上传插件是一款简化图片上传操作的插件。它提供了一些灵活的配置,能够帮助用户快速搭建起一个强大的图片分享平台。通过这些配置,用户还可以非常方便的同时上传多张图片,或将图片以指定的格式上传,免去手动转换图片格式的烦恼。 在最新版的图片上传控件中采用了全新的网络数据传输模块,新的模块全面优化了网络层的数据处理代码,同时在接收服务器返回的数据代码中采用精确识别的方式使数据处理效率更高。这些改进使图片上传控件具有了闪电般的上传速度。现在新颖网络图片上传控件在上传图片时平均每张图片帮助用户节省了50%的时间。 相信新颖网络图片上传控件能够为您的应用带来更好的用户体验。 产品特点如下: 1. 基于标准HTTP协议。 2. 支持BMP,JPG,GIF,PNG图片格式。 3. 支持自动生成缩略图。 4. 支持文件批量上传。 5. 支持文件拖拽操作。 6. 支持自定义上传信息。 7. 快速编辑。旋转操作。 8. 显示上传进度。 9. 支持文件格式批量转换。 10. 支持打开默认文件夹功能。 11. 免费提供JavaScript SDK包,方便您将插件快速集成到已有网站中。 支持语言:PHP,JSP,ASP,ASP.NET(C#),ASP.NET(VB),C++,VC,VC.NET,VB,VB.NET,C#,C#.NET,Delphi,C++Builder 支持平台:Visual Studio 6.0/2002/2003/2005/2008/2010,C++ Builder 6.0/2009/2010,Delphi 7/2009,Visual Basic 6.0/2008 支持脚本:JavaScript,VBScript 支持系统:Windows NT,Windows 2003,Windows XP,Windows Vista,Windows 7,Linux 支持浏览器:IE6,IE7,IE8,IE8(x64),IE9(x64),Firefox,Chrome,360安全浏览器,360极速浏览器,Maxthon1.x,Maxthon2.x,Maxthon3.x,QQ浏览器 支持图片格式:BMP,GIF,JPG,PNG,TIF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值