欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Multipart工作原理(文件上传到服务器)

程序员文章站 2022-04-20 21:05:15
...
Multipart工作原理


按照Multipart规定的格式将文件名和文件内容在请求中发送到服务器中

服务器从请求流中得到文件名和文件内容放入到Multipart定义的类中,
服务器处理请求中再在服务器生成相关的文件目录,并将内容保存到服务器中去



Spring applicationContext.xml配置文件:
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>




单文件方式实现:
HTML:
<form action="http://localhost:8080/v1/TestUploadImage" method="post" id="iconForm" enctype="multipart/form-data">
    <table>
    <tr>
        <td>头像</td>
        <td><input id="iconImg" name="iconImg" type="file"/></td>
        <td><input type="submit" value="上传"/></td>
    </tr>
    </table>
</form>



JAVA:
public static String uploadFile(String uploadDir, MultipartFile file, Boolean existReplace) {
		String logString = "FileIOUtils uploadFile : ";

		String returnMsg = null;

		String fileName = file.getOriginalFilename();
		logger.info(logString + "fileName == " + fileName);

		File targetFile = new File(uploadDir, fileName);
		logger.info(logString + "targetFile == " + targetFile);

		// 文件存在
		if (!targetFile.exists()) {
			logger.info(logString + "targetFile is not exist");
			// 新建文件目录
			targetFile.mkdirs();
		} else {
			logger.info(logString + "targetFile is exist");
			if (!existReplace) {
				logger.info(logString + "existReplace is false");
				returnMsg = "exist";
				return returnMsg; // 存在不进行替换,直接返回true
			}
		}

		// 保存文件
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			logger.error(logString + "targetFile save fail == " + e);
			returnMsg = "false";
			return returnMsg;
		}
		returnMsg = "true";
		logger.info(logString + "returnMsg ==  " + returnMsg);
		return returnMsg;
	}
}


方法1:
@RequestMapping(value = "/TestUploadImage", produces = "text/plain;charset=UTF-8")
public @ResponseBody String iconUpload(@RequestParam(value = "iconImg") MultipartFile iconImg,HttpServletRequest request,String aas ){
	MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;
	String fileName=multipartFile.getOriginalFilename();
	if (fileName==null){
		System.out.println("fileName==null");
	}

	if (fileName==""){
		System.out.println("fileName==''");
	}

	String tempfilename;
	if (System.getProperty("os.name").equals("Linux")) {
		tempfilename = "/opt/mcloudStat/";
	} else {
		tempfilename = "D:\\testImage";
	}

	returnMsg = FileIOUtils.uploadFile(tempfilename,multipartFile,false);
	return returnMsg;
}


方法2:
@RequestMapping(value = "/TestUploadImage", produces = "text/plain;charset=UTF-8")
public @ResponseBody String iconUpload(@RequestParam(value = "iconImg") MultipartFile iconImg,HttpServletRequest request,String aas ){
	MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;
	Map<String,MultipartFile> fileMap  =multipartRequest.getFileMap();

	String returnMsg = null;
	for(String key:fileMap.keySet()){
		System.out.println("key == " + key);

		if(key.equals("iconImg")){
			MultipartFile multipartFile = fileMap.get(key);

			String fileName=multipartFile.getOriginalFilename();

			if (fileName==null){
				System.out.println("fileName==null");
			}

			if (fileName==""){
				System.out.println("fileName==''");
			}

			System.out.println("fileName == " + fileName);

			String tempfilename;
			if (System.getProperty("os.name").equals("Linux")) {
				tempfilename = "/opt/mcloudStat/";
			} else {
				tempfilename = "D:\\testImage";
			}

			returnMsg = FileIOUtils.uploadFile(tempfilename,multipartFile,false);
		}
	}
	return returnMsg;
}



多文件方式实现:
HTML:
<form action="http://localhost:8080/v1/TestUploadImage" method="post" id="iconForm" enctype="multipart/form-data">
    <table>
    <tr>
        <td>头像</td>
        <td><input id="iconImg" name="iconImg" type="file"/></td>
        <td><input id="iconImg2" name="iconImg" type="file"/></td>//命字要一样,方便组成数组,会与排版顺序进行传入
        <td><input type="submit" value="上传"/></td>
    </tr>
    </table>
</form>



JAVA:
方法1:
@RequestMapping(value = "/TestUploadImage", produces = "text/plain;charset=UTF-8")
public @ResponseBody String iconUpload(@RequestParam(value = "iconImg") MultipartFile[] iconImg,HttpServletRequest request,String aas ){
	String returnMsg = null;
	int iconImgLen = iconImg.length;
	System.out.println("iconImgLen == " +iconImgLen);
	for(int i=0;i<iconImgLen;i++){
		String fileName=iconImg[i].getOriginalFilename();

		if (fileName==null){
			System.out.println("fileName==null");
		}

		if (fileName==""){
			System.out.println("fileName==''");
		}

		System.out.println("fileName == " + fileName);

		String tempfilename;
		if (System.getProperty("os.name").equals("Linux")) {
			tempfilename = "/opt/mcloudStat/";
		} else {
			tempfilename = "D:\\testImage";
		}

	        returnMsg = FileIOUtils.uploadFile(tempfilename,iconImg[i],false);
	}
	return returnMsg;
}



方法2:参考单单文方式实现方法2,不过HTML文件中的name要不一样

thumbnails可实现图片的压缩功能(PictureUtils.java)
http://blog.csdn.net/yaohucaizi/article/details/9718861