| | |
| | | package com.vci.starter.web.util; |
| | | |
| | | import com.vci.starter.web.pagemodel.PageHelper; |
| | | import com.vci.starter.web.constant.VConstant; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.http.MediaType; |
| | | |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLDecoder; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | |
| | | } |
| | | response.setContentType(contentType); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | //错误时也需要这个参数 |
| | | Cookie cookie = new Cookie("fileDownload", "true"); |
| | | cookie.setPath("/"); |
| | | response.addCookie(cookie); |
| | |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将错误的信息输入流写入到返回流中 |
| | | * @param response 响应对象 |
| | | * @param data 数据的信息 |
| | | * @throws IOException 拷贝出错的时候会抛出异常 |
| | | */ |
| | | public static void writeDataToResponse(HttpServletResponse response,String fileName,byte[] data,String contentType) throws IOException { |
| | | if (StringUtils.isBlank(contentType)) { |
| | | contentType = "application/force-download"; |
| | | } |
| | | response.setContentType(contentType); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | //错误时也需要这个参数 |
| | | response.addHeader("Content-Disposition", "attachment; filename="+ fileName+ ";filename*=utf-8''" + fileName); |
| | | Cookie cookie = new Cookie("fileDownload", "true"); |
| | | cookie.setPath("/"); |
| | | response.addCookie(cookie); |
| | | try { |
| | | response.getOutputStream().write(data); |
| | | } catch (IOException e) { |
| | | //有可能客户端的链接 |
| | | if (logger.isErrorEnabled()) { |
| | | logger.error("写入文件到响应流出错", e); |
| | | } |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将环境变量中的某个文件写到返回流中 |
| | | * @param response 响应对象 |
| | |
| | | * @param isQueryTotal 是否查询总数 |
| | | */ |
| | | public static void setQueryTotal(HttpServletRequest request, boolean isQueryTotal){ |
| | | WebThreadLocalUtil.getNeedQueryTotalInThread().set(isQueryTotal?"true":"false"); |
| | | WebThreadLocalUtil.setNeedQueryTotalInThread(isQueryTotal?"true":"false"); |
| | | //request.setAttribute(webProperties.getQueryTotalSessionName(), isQueryTotal); |
| | | } |
| | | |
| | |
| | | tempFileForDownloadMap.remove(uuid); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 内容编码 |
| | | * |
| | | * @param str 内容 |
| | | * @return 编码后的内容 |
| | | */ |
| | | public static String urlEncode(String str) { |
| | | try { |
| | | return URLEncoder.encode(str, VConstant.UTF8); |
| | | } catch (UnsupportedEncodingException e) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 内容解码 |
| | | * |
| | | * @param str 内容 |
| | | * @return 解码后的内容 |
| | | */ |
| | | public static String urlDecode(String str) { |
| | | try { |
| | | return URLDecoder.decode(str, VConstant.UTF8); |
| | | } catch (UnsupportedEncodingException e) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | } |
| | | } |