下载World/Excel/PDF/ZIP
代码:
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
//下载文件
@RequestMapping("/downLoadFile")
@Controller
public class Down {
@RequestMapping(value = "/Down.do",method = RequestMethod.GET)
public ResponseEntity pdfDownload(HttpServletRequest request) throws IOException {
ResponseEntity<byte[]> responseEntity = null;
ClassPathResource classPathResource = null;
try {
classPathResource = new ClassPathResource("projectFile/excel.xlsx");//读取下载的文件,可以为PDF/ZIP......
InputStream in =classPathResource.getInputStream();
final HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/pdf");
headers.add("Content-Disposition", "attachment; filename=" + classPathResource.getFilename());
responseEntity = new ResponseEntity<>(IOUtils.toByteArray(in), headers, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
}
return responseEntity;
}
}
文件位置: