springmvc上传文件(图片)

环境:springmvc+spring+mybatis
前提环境,需要把ssm框架搭建好,如果没有搭建的话可以先搭建这个框架

然后需要添加jar包
pom里面添加


    
    <dependency>
        <groupId>commons-iogroupId>
        <artifactId>commons-ioartifactId>
        <version>2.5version>
    dependency>
    
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.3.2version>
    dependency>
    
    <dependency>
        <groupId>commons-codecgroupId>
        <artifactId>commons-codecartifactId>
        <version>1.10version>
    dependency>

然后需要在springmvc.xml的配置文件中添加上传的配置文件

  
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
          
        <property name="defaultEncoding" value="utf-8" />    
          
        <property name="maxUploadSize" value="10485760000" />    
          
        <property name="maxInMemorySize" value="40960"/>    
    bean>   

然后就可以开干了,
首先是前台页面jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图片上传title>
head>
<body>
<form action="/eshop/upload/uploadimg" enctype="multipart/form-data" method="post">
    上传图片:<input type="file" name="file" value="选择图片"><br>
    <input type="submit" value="上传">
form>
body>
html>

后台的controller类:

package com.xiaopengpeng.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("upload")
public class UploadController { 
    @RequestMapping("uploadimg")
    public String uploadimg(MultipartFile file) throws Exception{
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        //写入本地磁盘
        InputStream is = file.getInputStream();  
        byte[] bs = new byte[1024];  
        int len;  
        OutputStream os = new FileOutputStream(new File("D:/temp/" + filename));  
        while ((len = is.read(bs)) != -1) {  
            os.write(bs, 0, len);  
        }  
        os.close();  
        is.close();  
        return "ok";
    }
}

完成!

上面的java类上传图片的时候使用的是原生态的上传形式,使用的是底层的java的io流的,其实他们提供封装好的保存形式,所以可以将上面的contriller类该成下面的这个类:

package com.xiaopengpeng.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("upload")
public class UploadController {
    @RequestMapping("toUploadimg")
    public String toUploadimg(){
        return "uploadimg";
    }

    @RequestMapping("uploadimg")
    public String uploadimg(MultipartFile file) throws Exception{
        String filename = file.getOriginalFilename();
        file.transferTo(new File("D:/temp/" + filename));

        return "ok";
    }

简单了许多吧,其实这个样子写功能是不完善的,如果不同用户上传的文件名字一样就会把前一个文件给覆盖了,多以名字不能这样就草草的结束,可以在名字上再下下功夫,这些就留给你们自己做吧

你可能感兴趣的:(springmvc上传文件(图片))