山一程--技术框架--web 技术
目的:汇集 Web 开发(Java 栈)常用代码片段。
1. download
场景:用户在前端请求下载 后台生成的文档. 例如:导出 excel.
1. @RestController, @GetMapping
2. 请求方法的入参 HttpServeltResponse
3. HttpServletResponse.reset();
4.exception , httpServletResponse.setHeader("Content-dispotion", "attachment; filename=" +
new String(( fileName + ".xlsx").getBytes("UTF-8"),"ISO-8850-1"));
5. OutputStream os = httpServletResponse.getOutputStream();
Document doc, doc.write(os) ; httpServletResponse.getOutputStream().flush()
6. close(). document.close(); httpServletReponse.getOutputStream().close(); os.close();

1 @GetMapping("/processResult/downloadResultExcel") 2 public void getRegProResultExportExcel(HttpServletResponse httpServletResponse) { 3 OutputStream os = null; 4 5 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 6 LocalDateTime now = LocalDateTime.now(); 7 String timeInfo = now.format(timeFormatter); 8 9 String fileName = "legacy performance" + timeInfo; 10 httpServletResponse.reset(); 11 try { 12 httpServletResponse.setHeader("Content-disposition", 13 "attachment;filename=" + 14 new String((fileName + ".xlsx").getBytes("UTF-8"), 15 "ISO-8859-1")); 16 os = httpServletResponse.getOutputStream(); 17 archiveDoc.write(os); 18 httpServletResponse.getOutputStream().flush(); 19 } catch (IOException ex) { 20 } finally { 21 try { 22 httpServletResponse.getOutputStream().close(); 23 archiveDoc.close(); 24 os.close(); 25 } catch (IOException ex) { 26 ex.printStackTrace(); 27 } 28 } 29 }
前端以 Extjs 为例
url link 直接设置为 GetMap(" xxx" )
windows.location.ref = link
1 var archiveResultWithExcel = "/legacy/regression/selection/archiveWithExcel"; 2 Ext.define("analytics.view.business.regression.regressionToolbar",{ 3 extend:"Ext.toolbar.Toolbar", 4 xtype:'regressionToolbar', 5 6 items:[ 7 { 8 id:'regressionUploadDataId', 9 text:'upload data', 10 iconCls:'x-fa fa-plus', 11 handler:'regressionProcess' 12 }, 13 { 14 id:'archiveResultWithExcelId', 15 text:'export excel', 16 iconCls:'x-fa fa-plus', 17 //disabled:false, 18 handler:function(){ 19 window.location.href = archiveResultWithExcel 20 } 21 /*handler: 'archiveResultWithExcel'*/ 22 } 23 ] 24 });


1 @RestController 2 @RequestMapping("/dataSource") 3 public class DataSourceController { 4 5 @PostMapping("/upload") 6 void uploadFile(HttpServletRequest request, 7 @ModelAttribute(value="file")MultipartFile uploadFile){ 8 9 String servletContextPath = request.getServletContext().getRealPath("/"); 10 String fileFullName = uploadFile.getOriginalFilename(); 11 12 int indexSuffix = fileFullName.indexOf("."); 13 String fileName = fileFullName.substring(0,indexSuffix); 14 15 // 将会在 spring boot 项目目录下,创建 temp 文件. 16 // aws AMI 环境 home/ec2-user/ 17 try { 18 File uploadedFile = File.createTempFile(fileName, ".txt", 19 Paths.get(servletContextPath).toFile()); 20 uploadFile.transferTo(uploadedFile); 21 }catch (Exception e){ 22 e.printStackTrace(); 23 } 24 }
Spring boot
在 Spring boot config 文件中设置了file-size-threshold, 不正确的情况下导致无法上传. 报 访问拒绝 的错误.

2022-12-08
背景:前端Angular14 , rest 发送 get 请求时,使用 `${this.url}/${userId}` , spring boot 后端 GetMapping(" xx ") 对应对象null.
solution 1: 前端 采用如下方式 ,object 包装



更改为 post


路径参数
@PathVariable @PathParam url / {id }
@RequestParam => header url? name =
@GetMapping("/demo/{id}")
public void demo( @PathVariable(name="id") String id, @RequestParam(name = "name") String name){ }
http://localhost:xx /demo/123?name=xxx
------------------------
@RequestBody body
http://localhost:8080/demo
{
"name:"dwdw",
"age": 18
}
@PostMapping(path = "/demo")
public void demo(@RequestBody Person person) { }
---------------
@RequestHeader
@CookieValue
--
@GetMapping("/demo")
public void demo( @RequestHeader(name = "myHeader") String myHeader,
@CookieValue(name = "myCookie") String myck){ }
----
@GetMapping("/demo")
public void demo(HttpServletRequest request){
request.getHeader("myHeader");
for(Cookie cookie : reuqest.gteCookies()){
if("myCookie".equals(cookie.getName())){
coolie.getValue())
}
---------------------




浙公网安备 33010602011771号