"Refactoring" implements JSP upload attachments using COS components

Source: Internet
Author: User
Tags cos

Using the Cos component to implement uploading attachments in JSP with Jsp&servlet refactoring project

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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>File Upload Demo</title><Linkhref= "/web01//css/main.css"rel= "stylesheet"type= "Text/css" /></Head><%@ include file="top.jsp" %><Body>    <formAction= "/web01/fileuploadcontroller"name= "Frm1"Method= "POST"enctype= "Multipart/form-data">file name:<inputtype= "File"name= "File1"size= " the"><BR>        <inputtype= "Submit"value= "Upload">    </form></Body><%@ include file="bottom.jsp" %></HTML>

Fileuploadcontroller.java

 PackageCom.test.IF.controller;Importjava.io.IOException;Importjava.util.Enumeration;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importcom.oreilly.servlet.MultipartRequest;ImportCom.oreilly.servlet.multipart.DefaultFileRenamePolicy;/*** Servlet Implementation class Fileuploadcontroller*/@WebServlet ("/fileuploadcontroller") Public classFileuploadcontrollerextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /**     * @seeHttpservlet#httpservlet ()*/     PublicFileuploadcontroller () {Super(); }    /**     * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { This. DoPost (request, response); }    /**     * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//path to save the fileString Savepath = "d:/test/uploadfile/"; intSizeLimit = 30 * 1024 * 1024; String FileName= ""; String Filesystemname= ""; LongFileSize = 0; Multipartrequest Multi=NewMultipartrequest (Request, Savepath, SizeLimit, "UTF-8",NewDefaultfilerenamepolicy ()); 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>File Upload Demo</title><Linkhref= "/web01//css/main.css"rel= "stylesheet"type= "Text/css" /></Head><%@ include file="top.jsp" %><Body>File Upload Successful! <BR>file name:<C:outvalue= "${requestscope.filesystemname}"></C:out><BR>File Size:<C:outvalue= "${requestscope.filesize}"></C:out></Body><%@ include file="bottom.jsp" %></HTML>

"Refactoring" implements JSP upload attachments using COS components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.