Servlet3.0上傳圖片樣本

來源:互聯網
上載者:User

標籤:cep   前端   表單提交   file   jsp   int   vax   port   response   

一、前端JSP頁面

<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet3.0實現檔案上傳</title>
</head>
<body>
<h1>Servlet3.0實現檔案上傳</h1>
<b id="msg" style="color: red;"></b>

<!-- 表單提交多媒體(此處指圖片)必須要加enctype="multipart/form-data"屬性和值 -->
<form name="myform" action="fileUpload1" method="post" enctype="multipart/form-data" onSubmit="return doCheck(this);">
<table>
<tr>
<td>使用者名稱:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>頭像:</td>
<td><input type="file" name="photo" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
var fm = document.forms[0];
//var form = document.forms[‘myform‘];
//var form = document.myform;//不能有重名的,否則不能擷取到
var msg = document.getElementById("msg");
function doCheck(fm) {
if (null == fm.username.value || "" == fm.username.value) {
msg.innerHTML = "* 使用者名稱不可為空!";
fm.username.focus();
return false;
}
if (null == fm.password.value || "" == fm.password.value) {
msg.innerHTML = "* 密碼不可為空!";
fm.password.focus();
return false;
}
if (null == fm.photo.value || "" == fm.photo.value) {
msg.innerHTML = "* 上傳的檔案不可為空!";
return false;
}
}
</script>
</body>
</html>

 

二、後台邏輯處理的Servlet,需要匯入檔案上傳處理包:commons-io -2.4.jar、commons -fileupload -1.3.1.jar

package com.qubo.servlet3x;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

@WebServlet("/fileUpload1")
@MultipartConfig
public class FileUploadServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Servlet3.0中,沒有提供直接擷取上傳檔案的檔案名稱方法。需要使用如下方式來擷取檔案名稱
*/
protected String getFileName(Part part) {
String fileName = null;
String cotentDesc = part.getHeader("Content-Disposition");
Pattern pattern = Pattern.compile("filename=\"(.+)\"");
Matcher matcher = pattern.matcher(cotentDesc);
if (matcher.find()) {
fileName = matcher.group(1);
}
return fileName;
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("username");
String password = request.getParameter("password");
Part part = request.getPart("photo");
String fileName = this.getFileName(part); //調用擷取檔案名稱的方法
String str = part.getName(); //前端jsp頁面中指定的name屬性的值,即<input type="file" name="photo" />中的photo
System.out.println("使用者名稱:" + name + ",密碼:" + password + ",檔案名稱:" + fileName + ",映像輸入框名:" + str);
String hzm = fileName.substring(fileName.indexOf("."));//擷取檔案的尾碼名
part.write("E:/ZhuoXun JavaWeb/Servlet3.x/WebContent/img/" + new Date().getTime() + hzm);//檔案的儲存位置
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

Servlet3.0上傳圖片樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.