新增上傳圖片
兩個JSP頁面
JSP頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>File控制元件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="doupload.jsp" enctype="multipart/form-data" method="post">
<p>姓名:<input type="text" name="user"></p>
<p>選擇圖片:<input type="file" name="nfile"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
JSP 頁面!
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上傳處理頁面</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String uploadFileName = ""; //上傳的檔名
String fieldName = ""; //表單欄位元素的name屬性值
//請求資訊中的內容是否是multipart型別
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//上傳檔案的儲存路徑(伺服器檔案系統上的絕對檔案路徑)
String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/" );
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//解析form表單中所有檔案
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
String owid = null;
String owname = null;
String owuser = null;
String owidCard = null;
String owpassword = null;
String owhouseNumber = null; 賦值
String owhouseType = null;
String owcheckData = null;
String nfile = null;
String owphone = null;
String owhouseNature = null;
while (iter.hasNext()) { //依次處理每個檔案
FileItem item = iter.next();
if (item.isFormField()){ //普通表單欄位
fieldName = item.getFieldName(); //表單欄位的name屬性值
if(fieldName.equals("owid")){
owid = item.getString("UTF-8");
}else if(fieldName.equals("owname")){
owname = item.getString("UTF-8");
}else if(fieldName.equals("owuser")){
owuser = item.getString("UTF-8");
}else if(fieldName.equals("owidCard")){
owidCard = item.getString("UTF-8");
}else if(fieldName.equals("owpassword")){ 獲取頁面值
owpassword = item.getString("UTF-8");
}else if(fieldName.equals("owhouseNumber")){
owhouseNumber = item.getString("UTF-8");
}else if(fieldName.equals("owhouseType")){
owhouseType = item.getString("UTF-8");
}else if(fieldName.equals("owcheckData")){
owcheckData = item.getString("UTF-8");
}else if(fieldName.equals("owphone")){
owphone = item.getString("UTF-8");
}else if(fieldName.equals("owhouseNature")){
owhouseNature = item.getString("UTF-8");
}
if (fieldName.equals("user")){
//輸出表單欄位的值
out.print(item.getString("UTF-8")+"上傳了檔案。<br/>");
}
}else{ //檔案表單欄位
String fileName = item.getName();
if (fileName != null && !fileName.equals("")) {
File fullFile = new File(item.getName());
File saveFile = new File(uploadFilePath, fullFile.getName());
item.write(saveFile);
uploadFileName = fullFile.getName();
out.print("上傳成功後的檔名是:"+uploadFileName);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>