spring MVC 上传图片 DEMO

原文地址为:spring MVC 上传图片 DEMO

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>


后台Controller

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
 */
@Controller
public 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);
    }
}




转载请注明本文地址:spring MVC 上传图片 DEMO
阅读更多
文章标签: spring
想对作者说点什么? 我来说一句

springMVC上传文件demo

2015年04月01日 28.35MB 下载

SpringMVC文件上传Demo代码

2017年03月01日 6KB 下载

mvc weui上传文件完整demo

2017年06月28日 13.64MB 下载

ASP.NET MVC 4 图片上传UploadDemo

2016年09月17日 11.88MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭