使用<input type="file">实现文件上传

来源:互联网 发布:php有cdn获取客户端ip 编辑:程序博客网 时间:2024/05/21 07:49

实现文件上传有很多方法,还有一些现成的插件可以使用,笔者在学习途中,并没有用过,读者可以百度一下。

笔者并没有实现文件过滤的功能,即限制文件的上传类型。

笔者使用了struts框架,需要在struts.xml文件的<struts></struts>标签内添加:

<!-- 设置上传文件的最大值 --><constant name="struts.multipart.maxSize" value="102400000"/>

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>  <constant name="struts.devMode" value="true"/>  <constant name="struts.multipart.maxSize" value="102400000"/>  <include file="struts-default.xml"></include>  <package name="default" extends="struts-default">    <action name="upload" class="file.action.UploadAction">      <result name="success">/success.jsp</result>    </action>  </package></struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><filter>  <filter-name>    struts2  </filter-name>  <filter-class>    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  </filter-class></filter><filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern></filter-mapping><welcome-file-list>  <welcome-file>upload.jsp</welcome-file></welcome-file-list></web-app>



upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %><html><head><title>upload</title></head><body> <!-- method必须设置成"post" --> <form action="upload.action" method="post" enctype="multipart/form-data">  <table>    <tr>      <td>Upload file:</td>      <td><input type="file" name="upload"></td>    </tr>    <tr>      <td colspan="2"><input type="submit" value="submit"></td>    </tr>  </table></form></body></html>

UploadAction.java

package file.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{private static final long serialVersionUID = 1L;        //设置文件的保存路径        private static final String PATH="D:\\JavaStudy\\J2EE\\Upload\\Upload\\WebContent\\";private File upload;private String uploadFileName;public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}        public String execute() throws Exception{                //得到输入流,通过struts已经得到名为upload的控件的值                InputStream is=new FileInputStream(upload);                //得到输出流                OutputStream os = new FileOutputStream(PATH+uploadFileName);        //分配接受缓冲区        byte buffer[] = new byte[1024];        int count=0;        while((count=is.read(buffer))>0){    os.write(buffer,0,count);        }        os.close();        is.close();        return "success";        }}

success.jsp

<%@ page language="java" %><html><head><title>success</title></head><body><img src="${request.uploadFileName}"></body></html>


0 0
原创粉丝点击