文件下载服务端代码示例
package com.yonyou.uap.um.servlet;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yonyou.uap.um.context.util.UmContextUtil;
import com.yonyou.uap.um.context.util.UmContextUtil.FsOperator;
import com.yonyou.uap.um.filestore.UmFileStoreException;
import com.yonyou.uap.ump.log.UmpLogger;
import com.yonyou.uap.ump.util.MAHomeFactory;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 5009692626436714447L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String user = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_USER));
user = new String(user.getBytes("ISO-8859-1"), "UTF-8");
String appid = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_APPID));
appid = new String(appid.getBytes("ISO-8859-1"), "UTF-8");
String token = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_TOKEN));
token = new String(token.getBytes("ISO-8859-1"), "UTF-8");
String devid = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_DEVID));
devid = new String(devid.getBytes("ISO-8859-1"), "UTF-8");
String fileName = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_FILENAME));
fileName = java.net.URLDecoder.decode(fileName, "ISO-8859-1");
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
if (fileName != null && !"".equalsIgnoreCase(fileName)) {
String maHome = MAHomeFactory.MA_HOME;
String fileDir = maHome + File.separator + "UPLOAD";
try {
downloadFile(fileDir, user, appid, resp, fileName);
} catch (Exception e) {
UmpLogger.error(e.getMessage());
}
}
}
/**
* download file for download servlet
*
* @param dir
* @param user
* @param appid
* @param response
* @param fileName
* @throws Exception
*/
public static Map<String, Object> downloadFile(String dir, String user, String appid, HttpServletResponse response, String fileName) throws Exception {
if (fileName == null || "".equalsIgnoreCase(fileName)) {
return null;
}
OutputStream output = null;
try {
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
response.setCharacterEncoding("UTF-8");
// 获得输出流
output = response.getOutputStream();
// 写入文件流
//...
Map map = new HashMap();
map.put("appid", appid);
map.put("filename", filename);
map.put("params", params);
map.put("hostURI", getHostUri());
byte[] bytes = ((ByteArrayOutputStream) output).toByteArray();
byte[] bese64Bytes = Base64.encodeBase64(bytes);
map.put(MAUtil.UPLOAD_KEY_FIELDS, new String(bese64Bytes));
return map;
} catch (Exception e) {
throw new UmFileStoreException("um download file exception", e);
} finally {
try {
if (output != null) {
output.close();
output = null;
}
} catch (Exception e) {
throw new UmFileStoreException( "um file stream close exception", e);
}
}
}
}
文档更新时间: 2018-01-16 10:09