为了账号安全,请及时绑定邮箱和手机立即绑定

为啥异常处理Exception不能写成SmartUploadException?

package com.imooc.servlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;


/**
 * Servlet implementation class SmartUploadServlet
 */
@WebServlet("/SmartUploadServlet")
public class SmartUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SmartUploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @throws ServletException 
	 * @throws IOException 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置文件保存路径
		String path = getServletContext().getRealPath("/")+"image";
		File file = new File(path);
		if(file.exists()){
			file.mkdirs();
		}
		SmartUpload su = new SmartUpload();
		String result = "文件上传成功";
		//初始化SmartUpload对象 getServletConfig()返回一个ServletConfig对象,通过该对象可以获得该servlet的初始化参数
		su.initialize(getServletConfig(),request, response);
		//设置上传文件的大小为10M
		su.setMaxFileSize(1024*1024*10);
		//设置上传文件的总大小为100M
		su.setTotalMaxFileSize(1024*1024*100);
		//设置允许上传文件的类型
		su.setAllowedFilesList("txt,jpg,png,gif");
		//设置不允许上传类型
		try {
//			su.setDeniedFilesList("jsp,js,rar");
			//上传文件
			su.upload();
			int count = su.save(path);
			System.out.println("上传成功了"+count+"个文件");
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			result = "上传失败";
//			e.printStackTrace();
		} catch (Exception e) {//这儿不能写成SmartUploadException 如果这样就会转发到异常信息页面而不是转发到02.jsp页面
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			if(e.getMessage().indexOf("1015")!=-1){
				result = "上传失败:上传文件类型不正确";
			}else if(e.getMessage().indexOf("1010")!=-1){
				result = "上传失败:上传文件类型不正确";
			}else if(e.getMessage().indexOf("1005")!=-1){
				result = "上传失败:大小超过单个上传文件的最大值";
			}else if(e.getMessage().indexOf("1110")!=-1){
				result = "上传失败:总大小超过所有上传文件的最大值";
			}
			e.printStackTrace();
		}
		request.setAttribute("result", result);
		request.getRequestDispatcher("jsp/02.jsp").forward(request, response);
	}

}


正在回答

2 回答

这个Exception是最大的异常 只要是出现异常就会打印堆栈,可是写成SmartException的话 一旦出现其他异常就还需要另外一个try catch。 代码看起来会很嵌套繁琐,这个只是练习所以这么写的

0 回复 有任何疑惑可以回复我~

这个Exception是最大的异常 只要是出现异常就会打印堆栈,可是写成SmartException的话 一旦出现其他异常就还需要另外一个try catch 代码看起来会很嵌套繁琐,这个只是练习所以这么写的

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为啥异常处理Exception不能写成SmartUploadException?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信