Rose里的简单上传下载

本文档介绍了在Rose框架中如何实现简单的文件上传和下载功能。首先,我们聚焦于如何创建一个上传页面,接着详细讲解Controller层如何处理上传请求。然后,我们将转向下载页面的实现,探讨如何设置响应头来正确触发文件下载。最后,我们会讨论相关安全性和性能优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.vm
<html>
    <title></title>
	<head >
    </head>
	<body >
		<form action="uploadFile" method="Post" enctype="multipart/form-data">
			<input name="file" type="file" id="file"/>
    		<br><br>
        	<input type="submit" value="开始长传"/>
		<form>
    </body>
</html>

2.下载页面

<html>
    <title></title>
	<head >
    </head>
	<body >
		$!{uploadFileName}
      	$!{length}
		$!{file}
      	<hr>
      	<a href="down?path=$!{path}/$!{uploadFileName}">下载</a>
    </body>
</html>


3.Controller

package com.shangchuan.controllers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import org.springframework.web.multipart.MultipartFile;

@Path("upload")
public class UploadController {
	
	public String shows() {
		return "upload";
	}
	
	@Post("uploadFile")
	public String uploadFile(Invocation inv, HttpServletRequest request, HttpServletResponse response, @Param("file") MultipartFile file) {
		String path = request.getSession().getServletContext().getRealPath("/upload");
		try {
			InputStream in;
			in = file.getInputStream();
			FileOutputStream fos = new FileOutputStream(new File(path, file.getOriginalFilename()));
			byte b[] = new byte[(int) file.getBytes().length];
			file.getInputStream().read(b);
			fos.write(b);
			fos.close();
			inv.addModel("uploadFileName", file.getOriginalFilename());// 上次信息文件名
			inv.addModel("length", file.getBytes().length);// 上次文件大小
			inv.addModel("path", path);
			inv.addModel("file", file);
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	@Get("down")
	public String down(Invocation inv, HttpServletResponse response, HttpServletRequest requ, @Param("path") String path) throws IOException {
		try {
			File file = new File(path);
			// 取得文件名。
			String filename = file.getName();
			System.out.println(filename + "  :filename");
			// 取得文件的后缀名。
			String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			// inline 在线打开
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			
			// response.addHeader("Content-Disposition",
			// "attachment;filename=filename.ext");
			response.addHeader("Content-Length", "" + file.length());
			// 当代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。
			// response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作。如下:
			// response.setHeader("Pragma", "No-cache");
			// response.setHeader("Cache-Control", "No-cache");
			// response.setDateHeader("Expires", 0);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");// 提示你是打开还是保存
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		return "";
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值