• 文件上传和下载


    controller 

    package com.imooc.web.controller;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Date;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.io.IOUtils;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import com.imooc.dto.FileInfo;
    @RestController
    @RequestMapping("/file")
    public class FileController {
    	private String folder = "C:\Users\65448\Desktop\spring-security-main\imooc-security-demo\src\main\java\com\imooc\web\controller";
    	@PostMapping
    	public FileInfo upload(MultipartFile file) throws Exception {
    
    		System.out.println(file.getName());
    		System.out.println(file.getOriginalFilename());
    		System.out.println(file.getSize());
    		File localFile = new File(folder, new Date().getTime() + ".txt");
    		file.transferTo(localFile);
    		return new FileInfo(localFile.getAbsolutePath());
    	}
    	@GetMapping("/{id}")
    	public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
    		try (InputStream inputStream = new FileInputStream(new File(folder, id + ".txt"));
    				OutputStream outputStream = response.getOutputStream();) {
    			response.setContentType("application/x-download");
    			response.addHeader("Content-Disposition", "attachment;filename=test.txt");
    			
    			IOUtils.copy(inputStream, outputStream);
    			outputStream.flush();
    		} 
    
    	}
    
    }
    

      测试类

    package com.imooc.web.controller;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.mock.web.MockMultipartFile;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.util.Date;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserControllerTest {
    
        @Autowired
        private WebApplicationContext wac;
        // 伪造http请求 测试
        private MockMvc mockMvc;
    
        /**
         * 执行测试用例之前会先执行
         */
        @Before
        public void setup() {
            mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
    
        @Test
        public void whenUploadSuccess() throws Exception {
            String result = mockMvc.perform(fileUpload("/file")
                    .file(new MockMultipartFile("file", "test.txt", "multipart/form-data", "hello upload".getBytes("UTF-8"))))
                    .andExpect(status().isOk())
                    .andReturn().getResponse().getContentAsString();
            System.out.println(result);
        }
    }
    

      

  • 相关阅读:
    2020年Android面试题含答案
    flutter系列(一)----- 开发环境搭建
    Android应用安全防护和逆向分析 ——apk混淆成其他语言代码
    Android应用安全防护和逆向分析 ——apk反编译
    Android中 TextView 加载 混合字符 自动换行解决方案
    H5跳转app本地的规则定义
    Android ListView 九大重要属性详细分析
    ListView和ScrollView滑动到顶部
    简要的汇总Android
    关于ViewPager+Fragment中Fragment不销毁/生命周期
  • 原文地址:https://www.cnblogs.com/412013cl/p/14053665.html
Copyright © 2020-2023  润新知