Explorar el Código

完成文件上传范例的初始版本,支持单个文件和多个文件上传。

huangbo %!s(int64=6) %!d(string=hace) años
padre
commit
e365aa7af3

+ 5 - 0
ipu-rest-demo/readme.md

@ -23,3 +23,8 @@ control:请求的分发。
23 23
service:业务逻辑。
24 24
dao:数据库逻辑。
25 25
service+dao后续可以移植到dubbo服务中。
26

27
六、问题排查记录
28
6.1 异常:request part 'file' is not present
29
原因:指定了上传文件的处理器CommonsMultipartResolver。
30
解决方法:注释掉,不指定文件处理器即可。

+ 52 - 41
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/data/FileUploadController.java

@ -1,58 +1,69 @@
1 1
package com.ai.ipu.server.demo.control.data;
2 2

3
import java.io.File;
4 3
import java.io.IOException;
5 4

6 5
import org.springframework.stereotype.Controller;
7 6
import org.springframework.web.bind.annotation.RequestMapping;
8 7
import org.springframework.web.bind.annotation.RequestMethod;
8
import org.springframework.web.bind.annotation.RequestParam;
9
import org.springframework.web.bind.annotation.ResponseBody;
9 10
import org.springframework.web.multipart.MultipartFile;
10 11

11
import com.ai.ipu.restful.web.ServletManager;
12
import com.ai.ipu.basic.util.IpuUtility;
13
import com.ai.ipu.data.JMap;
14
import com.ai.ipu.data.impl.JsonMap;
15
import com.ai.ipu.restful.util.MultipartFileUtils;
12 16

17
/**
18
 * @author huangbo@asiainfo.com
19
 * @team IPU
20
 * @date 2019年1月6日下午10:44:42
21
 * @desc a)注解@RequestParam来获取表单
22
 * b)transferTo(File file)方法来转存文件到指定路径
23
 */
13 24
@Controller
14
@RequestMapping("/file")
25
@RequestMapping("/upload")
15 26
public class FileUploadController {
16 27

28
    @ResponseBody
29
    @RequestMapping(value="/mult", method=RequestMethod.POST)
30
    public JMap uploadMultFile(@RequestParam("files") MultipartFile[] mFile) throws IOException {
31
        if (mFile == null || mFile.length == 0) {
32
            IpuUtility.error("上传表单中无文件");
33
        }
34
        for (int i = 0; i < mFile.length; i++) {
35
            System.out.println(MultipartFileUtils.takeFileInfo(mFile[i]));
36
            /*
37
             * 具体的文件处理逻辑
38
             */
39
        }
40
        
41
        JMap result = new JsonMap();
42
        result.put("msg", "上传多个文件成功");
43
        result.put("file_count", mFile.length);
44
        return result;
45
    }
17 46
    
18
    @RequestMapping(value="/upload", method=RequestMethod.POST)
19
    public String upload(MultipartFile[] mfile) throws IOException {
20
        if (mfile !=null && mfile.length>0) {
21
            for (int i = 0;i<mfile.length;i++){
22
                long start = System.currentTimeMillis();
23
                System.out.println("-------------------------------------------------");
24
                System.out.println("获取文件流"+mfile[i].getInputStream());
25
                System.out.println("获取文件的字节大小【byte】"+mfile[i].getSize());
26
                System.out.println("文件类型"+mfile[i].getContentType());
27
                System.out.println("获取文件数据"+mfile[i].getBytes());
28
                System.out.println("文件名字"+mfile[i].getName());
29
                System.out.println("获取上传文件的原名"+mfile[i].getOriginalFilename());
30

31
                System.out.println("-------------------------------------------------");
32

33
                try {
34
                    String filePath = ServletManager.getSession().getServletContext()
35
                            .getRealPath("/") + "assets/" +start+ mfile[i].getOriginalFilename();
36

37
                    //转存文件
38

39
                    mfile[i].transferTo(new File(filePath));
40
                }catch (Exception e){
41
                    e.printStackTrace();
42
                }
43

44
                //mfile[i].transferTo(new File("D:/ideas/"+mfile[i].getOriginalFilename()+ mfile[i].getOriginalFilename().substring(
45
                     //   mfile[i].getOriginalFilename().lastIndexOf("."))));
46
                // System.out.println(mfile.getOriginalFilename());
47
              //  mfile[i].transferTo(new File("/assets" + mfile[i].getOriginalFilename()));
48

49
            } 
50
        } else {
51
            System.out.println("失败");
47
    /**
48
     * @author huangbo@asiainfo.com
49
     * @title: uploadSingleFile
50
     * @desc: 单个文件上传范例
51
     */
52
    @ResponseBody
53
    @RequestMapping(value="/single")
54
    public JMap uploadSingleFile(@RequestParam("file") MultipartFile mFile){
55
        if(mFile==null){
56
            IpuUtility.error("上传表单中无文件");
57
        }
58
        if (mFile.isEmpty()){
59
            IpuUtility.error("上传文件内容为空");
52 60
        }
53
        return "";
54
        /*ModelAndView mv = new ModelAndView();
55
        mv.setViewName("other/home");
56
        return mv;*/
61
        System.out.println(MultipartFileUtils.takeFileInfo(mFile));
62
        /*
63
         * 具体的文件处理逻辑
64
         */
65
        JMap result = new JsonMap();
66
        result.put("msg", "上传单个文件成功");
67
        return result;
57 68
    }
58 69
}

+ 0 - 3
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/ui/DemoController.java

@ -12,8 +12,6 @@ import javax.servlet.http.HttpSession;
12 12
import org.springframework.stereotype.Controller;
13 13
import org.springframework.web.bind.annotation.RequestMapping;
14 14

15
import com.ai.ipu.restful.annotation.IpuDataCache;
16

17 15
/**
18 16
 * @author huangbo
19 17
 * @date 2017年11月26日 下午11:27:22
@ -45,7 +43,6 @@ public class DemoController {
45 43
	 * 标准访问方式:HttpServletRequest和HttpServletResponse
46 44
	 * 跳转页面,使用了自定义截器
47 45
	 */
48
	@IpuDataCache
49 46
	@RequestMapping(value = "/redirect")
50 47
	public void redirectDemo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51 48
		response.sendRedirect("http://www.baidu.com");

+ 7 - 7
ipu-rest-demo/src/main/resources/ipu-spring-mvc.xml

@ -13,19 +13,19 @@
13 13
    <context:component-scan base-package="com.ai.ipu.server.demo" />
14 14
    
15 15
    <!-- 定义文件上传解析器 -->
16
	<bean id="multipartResolver"
16
	<!-- <bean id="multipartResolver"
17 17
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
18
		<!-- 设定默认编码 -->
18
		设定默认编码
19 19
		<property name="defaultEncoding" value="UTF-8"></property>
20
		<!-- 设定文件上传的最大值为5MB,5*1024*1024 -->
20
		设定文件上传的最大值为5MB,5*1024*1024
21 21
		<property name="maxUploadSize" value="5242880"></property>
22
		<!-- 设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240 -->
22
		设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240
23 23
		<property name="maxInMemorySize" value="40960"></property>
24
		<!-- 上传文件的临时路径 -->
24
		上传文件的临时路径
25 25
		<property name="uploadTempDir" value="fileUpload/temp"></property>
26
		<!-- 延迟文件解析 -->
26
		延迟文件解析
27 27
		<property name="resolveLazily" value="true" />
28
	</bean>
28
	</bean> -->
29 29
    
30 30
    <!-- 不处理静态资源 -->
31 31
    <mvc:default-servlet-handler/>