SSM项目文件上传和下载

SSM项目文件### 导入相关依赖(pom.xml)

<!--        文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

编写相关的Controller(FileController.java)

@Controller
public class FileController{

  @RequestMapping("/upload")
  @ResponseBody
  public String upload (@Param("file") CommonsMultipartFile file) throws IOException{
    String uploadFileName = file.getOriginalFilename();
    String uuid = UUID.randomUUID().toString().replace("-", "");
    JSONObject object = new JSONObject();
    ObjectMapper mapper = new ObjectMapper();
    if ("".equals(uploadFileName)) {
      object.put("code",0);
      object.put("message","上传失败!请重试!");
      return mapper.writeValueAsString(object);
    }
    System.out.println("上传文件:"+uploadFileName);
    //填写保存文件的地址
    String path = "******";
    File realPath = new File(path);
    if (!realPath.exists()){
      realPath.mkdir();
    }
    System.out.println("上传文件保存地址:"+realPath);

    InputStream inputStream = file.getInputStream();//文件输入流
    OutputStream outputStream = new FileOutputStream(new File(realPath,uuid+uploadFileName));//文件输出流

    //读写
    int len = 0;
    byte[] buffer = new byte[10240];
    while ((len=inputStream.read(buffer)) != -1){
      outputStream.write(buffer,0,len);
      outputStream.flush();
    }
    outputStream.close();
    inputStream.close();
    String file_path = realPath+"/"+uuid+uploadFileName;
    object.put("code",1);
    object.put("message","上传文件成功!");
    return mapper.writeValueAsString(object);
    }
}

编写测试页面(file.html)

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" accesskey=".xlsx">
    <input type="submit">
</form>

文件下载

导入相关依赖(pom.xml)

<!--        文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

编写相关的Controller(FileController.java)

@Controller
public class FileController{

  @RequestMapping("/download")
    public String downloads(HttpServletResponse response,
                            @Param("fileName")String fileName) throws Exception{
        //下载文件的地址
        String path = "******";

        //设置response响应头
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("multipart/form-date");//二进制传输数据

        //设置响应头
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
        File file = new File(path,fileName);

        //读取文件--输入流
        InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();

        byte[] buffer = new byte[10240];
        int index = 0;
        while ((index = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer,0,index);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
        return null;
    }
}

编写测试页面(file.html)

<form action="/download" method="post" enctype="multipart/form-data">
    <input type="text" name="fileName" value="Teacher_Info_Template.xlsx">
    <input type="submit" value="点击下载模版">
</form>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇