| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.lf.server.entity.all.HttpStatus; |
| | | import com.lf.server.entity.all.ResponseMsg; |
| | | import com.lf.server.entity.all.SettingData; |
| | | import com.lf.server.entity.all.StaticData; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.net.URLEncoder; |
| | | import java.sql.Timestamp; |
| | | import java.util.Calendar; |
| | | import java.util.Random; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取错误JSON |
| | | */ |
| | | public static String getErrJson(HttpStatus status, String msg) { |
| | | return JSON.toJSONString(new ResponseMsg<String>(status, msg)); |
| | | } |
| | | |
| | | /** |
| | | * 写响应信息 |
| | | */ |
| | | public static void writeInfo(HttpStatus status, String info, HttpServletResponse res) { |
| | | try { |
| | | WebHelper.write2Page(res, WebHelper.getErrJson(status, info)); |
| | | } catch (Exception e) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取随机整数 |
| | | */ |
| | | public static int getRandomInt(int min, int max) { |
| | | return new Random().nextInt(max) % (max - min + 1) + min; |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param file 文件 |
| | | * @param fileName 文件名 |
| | | * @param res 响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static void download(String file, String fileName, HttpServletResponse res) throws Exception { |
| | | if (StringHelper.isNull(fileName)) { |
| | | fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8"); |
| | | } |
| | | |
| | | // 设置响应头中文件的下载方式为附件方式,以及设置文件名 |
| | | res.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
| | | // 设置响应头的编码格式为UTF-8 |
| | | res.setCharacterEncoding("UTF-8"); |
| | | |
| | | // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8") |
| | | String ext = FileHelper.getExtension(file); |
| | | String mime = FileHelper.getMime(ext); |
| | | res.setContentType(mime); |
| | | |
| | | // 通过response对象,获取到输出流 |
| | | ServletOutputStream outputStream = res.getOutputStream(); |
| | | // 定义输入流,通过输入流读取文件内容 |
| | | FileInputStream fileInputStream = new FileInputStream(file); |
| | | |
| | | int len = 0; |
| | | byte[] bytes = new byte[1024]; |
| | | while ((len = fileInputStream.read(bytes)) != -1) { |
| | | // 通过输入流读取文件数据,然后通过上述的输出流写回浏览器 |
| | | outputStream.write(bytes, 0, len); |
| | | outputStream.flush(); |
| | | } |
| | | |
| | | // 关闭资源 |
| | | outputStream.close(); |
| | | fileInputStream.close(); |
| | | } |
| | | } |