注意保持action中File的变量和jsp中input的name名相同。
action中定义变量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import com.opensymphony.xwork2.ActionSupport; public class UploadFileAction extends ActionSupport { private File upload; private String uploadFileName; private String uploadContentType; public String execute() throws Exception { FileInputStream fis = new FileInputStream(upload); File file = new File("E:/dsideal/upload/" + uploadFileName); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[8912]; int count = 0; while((count = fis.read(buffer)) != -1) { fos.write(buffer, 0, count); } fos.close(); fis.close(); return SUCCESS; } }
|
jsp页面:
1 2 3 4 5
| <s:form action="mystruts/uploadFile.action" enctype="multipart/form-data"> <s:file name="upload" label="输入要上传的文件名字" /> <s:submit value="上传" /> </s:form> </body>
|
使用了struts标签,也可以直接使用html表单标签form、input