java 去掉TXT文本文件的bom头信息


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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值