實現文件上傳有很多方法,還有一些現成的插件可以使用,筆者在學習途中,並沒有用過,讀者可以百度一下。
筆者並沒有實現文件過濾的功能,即限制文件的上傳類型。
筆者使用了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>