第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為啥異常處理Exception不能寫(xiě)成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
		//設(shè)置文件保存路徑
		String?path?=?getServletContext().getRealPath("/")+"image";
		File?file?=?new?File(path);
		if(file.exists()){
			file.mkdirs();
		}
		SmartUpload?su?=?new?SmartUpload();
		String?result?=?"文件上傳成功";
		//初始化SmartUpload對(duì)象?getServletConfig()返回一個(gè)ServletConfig對(duì)象,通過(guò)該對(duì)象可以獲得該servlet的初始化參數(shù)
		su.initialize(getServletConfig(),request,?response);
		//設(shè)置上傳文件的大小為10M
		su.setMaxFileSize(1024*1024*10);
		//設(shè)置上傳文件的總大小為100M
		su.setTotalMaxFileSize(1024*1024*100);
		//設(shè)置允許上傳文件的類型
		su.setAllowedFilesList("txt,jpg,png,gif");
		//設(shè)置不允許上傳類型
		try?{
//			su.setDeniedFilesList("jsp,js,rar");
			//上傳文件
			su.upload();
			int?count?=?su.save(path);
			System.out.println("上傳成功了"+count+"個(gè)文件");
//		}?catch?(SQLException?e)?{
//			//?TODO?Auto-generated?catch?block
//			result?=?"上傳失敗";
//			e.printStackTrace();
		}?catch?(Exception?e)?{//這兒不能寫(xiě)成SmartUploadException?如果這樣就會(huì)轉(zhuǎn)發(fā)到異常信息頁(yè)面而不是轉(zhuǎn)發(fā)到02.jsp頁(yè)面
			//?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?=?"上傳失敗:大小超過(guò)單個(gè)上傳文件的最大值";
			}else?if(e.getMessage().indexOf("1110")!=-1){
				result?=?"上傳失?。嚎偞笮〕^(guò)所有上傳文件的最大值";
			}
			e.printStackTrace();
		}
		request.setAttribute("result",?result);
		request.getRequestDispatcher("jsp/02.jsp").forward(request,?response);
	}

}


正在回答

2 回答

這個(gè)Exception是最大的異常 只要是出現(xiàn)異常就會(huì)打印堆棧,可是寫(xiě)成SmartException的話 一旦出現(xiàn)其他異常就還需要另外一個(gè)try catch。 代碼看起來(lái)會(huì)很嵌套繁瑣,這個(gè)只是練習(xí)所以這么寫(xiě)的

0 回復(fù) 有任何疑惑可以回復(fù)我~

這個(gè)Exception是最大的異常 只要是出現(xiàn)異常就會(huì)打印堆棧,可是寫(xiě)成SmartException的話 一旦出現(xiàn)其他異常就還需要另外一個(gè)try catch 代碼看起來(lái)會(huì)很嵌套繁瑣,這個(gè)只是練習(xí)所以這么寫(xiě)的

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Java中的文件上傳下載
  • 參與學(xué)習(xí)       77386    人
  • 解答問(wèn)題       360    個(gè)

由簡(jiǎn)入深,輕松實(shí)現(xiàn)文件上傳下載功能及相關(guān)框架的使用

進(jìn)入課程

為啥異常處理Exception不能寫(xiě)成SmartUploadException?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)