利用jsp&Servlet重构项目 利用 cos 组件实现jsp中上传附件
fileUpload.jsp --> FileUploadController.java --> fileUploadResult.jsp
fileUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件上传demo</title><link href="/web01//CSS/main.css" rel="stylesheet" type="text/css" /></head><%@ include file="top.jsp" %><body> <form action="/web01/fileUploadController" name="frm1" method="post" enctype="multipart/form-data"> 文件名:<input type="file" name="file1" size="50"><br> <input type="submit" value="上传"> </form></body><%@ include file="bottom.jsp" %></html>
FileUploadController.java
package com.test.IF.controller;import java.io.IOException;import java.util.Enumeration;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.oreilly.servlet.MultipartRequest;import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;/** * Servlet implementation class FileUploadController */@WebServlet("/fileUploadController")public class FileUploadController extends HttpServlet { PRivate static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FileUploadController() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 文件要保存的路径 String savePath = "d:/test/uploadfile/"; int sizeLimit = 30 * 1024 * 1024; String fileName = ""; String fileSystemName = ""; long fileSize = 0; MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit, "UTF-8", new DefaultFileRenamePolicy()); Enumeration fileNames = multi.getFileNames(); while (fileNames.hasMoreElements()) { fileName = (String) fileNames.nextElement(); fileSystemName = multi.getFilesystemName(fileName); if (fileSystemName != null) { fileSize = multi.getFile(fileName).length() / 1024; } } request.setAttribute("fileSystemName", fileSystemName); request.setAttribute("fileSize", fileSize); request.getRequestDispatcher("/view/fileUploadResult.jsp").forward(request, response); }}
fileUploadResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件上传demo</title><link href="/web01//css/main.css" rel="stylesheet" type="text/css" /></head><%@ include file="top.jsp" %><body>文件上传成功!<br>文件名:<c:out value="${requestScope.fileSystemName }"></c:out><br>文件大小:<c:out value="${requestScope.fileSize }"></c:out></body><%@ include file="bottom.jsp" %></html>
新闻热点
疑难解答