import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TestDownControlelr {
@Test
public ResponseEntity<byte[]> down(HttpSession session) throws IOException {
//获取下载文件的路径
String realPath = session.getServletContext().getRealPath("img");
String finalPath = realPath + File.separator + "2.jpg";
FileInputStream is = new FileInputStream(finalPath);
//available(): 获取输入流所读取的文件的最大字节数
byte[] b = new byte[is.available()];
is.read(b);
//设置请求头
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition","attachment;filename=zzz.jpg");
//设置响应状态
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, statusCode);
return entity;
}
}