import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class DownloadYhsc extends HttpServlet {
/**
* @author admin
* @version 2.0
*/
private static final long serialVersionUID = 8529206527876956262L;
final protected static Logger logger = LoggerFactory
.getLogger(Download.class);
@Autowired
private ExportExcelUtil exportExcelUtil;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@Override
@SuppressWarnings({ "unchecked" })
public void doGet(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> paramMap = request.getParameterMap();
if (null == paramMap || paramMap.isEmpty()) {
return;
}
String serverPath = new StringBuilder()
.append(ArteryUtil.getRequest().getSession()
.getServletContext().getRealPath(File.separator))
.append("pub\\yhsc").append(File.separator).toString();
String fileName;
try {
fileName = URLEncoder.encode("xxx.docx", "UTF-8");
String header = new StringBuilder().append("attachment;filename=")
.append(fileName).toString();
response.setContentType("application/octet-stream");
response.addHeader("Content-disposition", header);
response.addHeader("Transfer-Encoding", null);
response.addHeader("connection", "close");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File file = new File(serverPath + "xxx.docx");
FileInputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = response.getOutputStream();
IOUtils.copy(in, out);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error("文件不存在" + e);
} catch (IOException e) {
e.printStackTrace();
logger.error("获取流异常" + e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
}
/**
* 转换编码
*
* @param fileName
* @return
*/
private String getDecodedStr(String str) {
String decodedStr = null;
try {
if (null == str || str.isEmpty()) {
decodedStr = StringUtils.EMPTY;
} else {
decodedStr = URLDecoder.decode(str, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("不支持的编码" + e);
}
return decodedStr;
}
/**
* Destruction of the servlet. <br>
*/
@Override
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
@Override
public void init() throws ServletException {
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}