使用<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
- 使用<input type="file">实现文件上传
- 使用input type="file"上传文件
- input type=file 只读,并能实现文件上传
- js 实现 <input type="file" /> 文件上传
- 使用input type=file 上传文件时需注意
- 使用input type=file 上传文件时需注意
- 使用input type=file 上传文件时需注意
- 使用input type=file 上传文件时需注意
- 同时实现文件上传input type =file和文本input type=text
- input type=file 服务器端上传文件
- <input type="file"/> 文件上传
- <input type='file'>选择文件后自动上传文件
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- Android:让WebView支持<input type="file"…>元素,实现文件上传
- html5 中input type=‘file’实现文件图片等的上传的文件类型限制
- 关于 input【type=file】文件上传控件实现自定义文字及实时图片预览
- 通过<input type=“file”>上传文件问题
- (1.4.6.2)中位数
- VS2013跟友元有仇
- React实践笔记-Quick Start
- 刷一题Leetcode:Ugly Number
- Swift开发 可选类型Optional
- 使用<input type="file">实现文件上传
- Js中的this和window.event.srcElement
- 网络各层协议
- 许愿网
- React实践系列笔记-JSX
- (4.1.27.2)使用AndroidStudio编译NDK的方法及错误解决方案
- eclipse启动报i386\jvm.cfg
- 多重集组合计数
- iOS条件编译之判断当前编译环境是不是arc