spring MVC 上传图片 DEMO
来源:互联网 发布:怎样做数据透视表 编辑:程序博客网 时间:2024/05/14 11:47
HTML 页面:
<%-- Created by IntelliJ IDEA. User: john Date: 14-9-1 Time: 下午4:49 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title></title></head><body><form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <td width="100" align="right">照片:</td> <td><input type="file" name="studentPhoto"/> <input type="submit"></td> </tr> </table></form></body></html>
package com.gochaintv.copyright.controller;import com.gochaintv.copyright.util.FileUpload;import org.apache.commons.io.FileUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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 javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.io.IOException;import java.io.OutputStream;/** * Created by shhao. * Date: 14-9-1 * Time:下午4:32 */@Controllerpublic class FileUploadController { Logger logger = LoggerFactory.getLogger(FileUploadController.class); @RequestMapping("upload") public void upload(@RequestParam("studentPhoto") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { String filePath = FileUpload.uploadFile(file, request); logger.info("filePath:" + filePath); response.setContentType("text/html;charset=utf8"); response.getWriter().write("<img src='"+filePath+"'/>"); } @RequestMapping("download") public void download(String fileName, HttpServletResponse response) throws IOException { OutputStream os = response.getOutputStream(); try { response.reset(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("image/jpeg; charset=utf-8"); os.write(FileUtils.readFileToByteArray(FileUpload.getFile(fileName))); os.flush(); } finally { if (os != null) { os.close(); } } }}
package com.gochaintv.copyright.util;import org.apache.commons.io.FileUtils;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.util.Date;/** * Created by shhao. * Date: 14-9-1 * Time:下午4:12 */public class FileUpload { public static final String FILE_PATH = "/upload/"; //文件上传 public static String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException { String fileName = file.getOriginalFilename(); File tempFile = new File(FILE_PATH, new Date().getTime() + String.valueOf(fileName)); if (!tempFile.getParentFile().exists()) { tempFile.getParentFile().mkdir(); } if (!tempFile.exists()) { tempFile.createNewFile(); } file.transferTo(tempFile); return "/download?fileName=" + tempFile.getName(); } public static File getFile(String fileName) { return new File(FILE_PATH, fileName); }}
1 7
- spring MVC 上传图片 DEMO
- spring mvc 图片上传
- spring mvc 上传图片
- Spring MVC 图片上传
- Spring mvc图片上传
- spring mvc 上传图片
- spring mvc 图片上传
- spring mvc 图片上传
- spring mvc 上传图片
- Spring MVC-图片上传
- Spring MVC上传图片
- Spring Mvc 上传图片代码
- spring mvc+xheditor图片上传
- 使用Spring MVC上传图片
- form图片上传+spring MVC
- spring mvc实现图片上传
- spring mvc 图片异步上传
- Spring mvc批量图片上传
- Python字典 (dictionary)
- php 全角数字转半角 与 手机号码验证
- Java中自定义枚举(Enum)项的值及int和Enum的互相转换
- Android Button事件触发机制介绍
- MySQLdb使用
- spring MVC 上传图片 DEMO
- SQL使用递归实现数据的累加
- MediaRecorder test
- Ant builder.xml详解
- 通过GetModuleHandleEx 得到函数调用者所在的DLL/EXE
- 关于hive RegexSerDe的源码分析
- GRUB启动命令详解
- 税的取数SQL
- linux下创建用户