import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
* 一级标签批量添加(上传 *.txt 文件的方式)
* <p>Title:CMS</p>
* <p>Description:TODO</p>
* <p>Copyright (C): 2013</p>
* <p>Company:Huawei</p>
* <p>Date:Jun 25, 2013</p>
* @author:bKF51722
*/
public class UploadLabelServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4129735769163441128L;
/**
* 日志工厂
*/
private static final DebugLog log = LogFactory.getDebugLog("CONTENTTAG");
/**
* Constructor of the object.
*/
public UploadLabelServlet() {
super();
}
/**
* Servlet配置对象
*/
private ServletConfig config;
/**
* 销毁
* Destruction of the servlet. <br>
*/
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
*/
public void init(ServletConfig configs) throws ServletException {
this.config = configs;
}
/**
* Servlet的POST处理方法
* @param request HTTP请求对象
* @param response HTTP请求响应对象
* @throws ServletException
* @throws IOException
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//设置返回消息
String msg = "message";
//管理上传对象
SmartUpload labelUpload = new SmartUpload();
//初始化上传对象
labelUpload.initialize(config, request, response);
//从request请求中获取session
HttpSession session = request.getSession(false);
//防止登录超时引起空指针异常
if(null == session || "".equals(session))
{
//重定向到登录页面
response.sendRedirect(request.getContextPath()+"/admin/adminlogin.action");
}
//创建本地临时目录
String capLocalPath = Path.getWebRootPath() + "txtLabelNameFileTemp";
FileHelper.createDir(capLocalPath);
//得到session里面存放的用户信息
AdminInfo admin = (AdminInfo) session.getAttribute(
Constant.SESSION_KEY_ADMIN_INFO);
//操作员
String operator = admin.getOperator();
String fatherNodeId ="";
try {
//设置文件上传大小的限制(设置为3M)
labelUpload.setMaxFileSize((1024*1024)*3L);
//执行上传操作
labelUpload.upload();
//从页面获取父节点ID
fatherNodeId = labelUpload.getRequest().getParameter("fatherNodeId");
if("".equals(fatherNodeId) || null == fatherNodeId)
{
request.setAttribute(msg, "父节点为空!:"+fatherNodeId);
request.setAttribute("backUrl", "admin/qeuryTagListPage.action?search=no&tagid="+fatherNodeId);
//跳转到上传失败页面,提示用户上传失败:父节点为空!
request.getRequestDispatcher("/WEB-INF/jsp/ums/admin/contenttag/uploadError.jsp").forward(request, response);
return;
}
}
catch (SmartUploadException upe)
{
request.setAttribute(msg, "文件上传出现异常!:"+upe.getMessage());
request.setAttribute("backUrl", "admin/qeuryTagListPage.action");
//跳转到上传失败页面,提示用户上传失败:文件上传出现异常!
request.getRequestDispatcher("/WEB-INF/jsp/ums/admin/contenttag/uploadError.jsp").forward(request, response);
return;
}
catch (IOException ioe)
{
request.setAttribute(msg, "文件读写错误!:"+ioe.getMessage());
request.setAttribute("backUr
java 去掉TXT文本文件的bom头信息
最新推荐文章于 2024-04-14 21:02:49 发布