Struts文件上传功能的实现
来源:互联网 发布:ubuntu怎么卸载jdk 编辑:程序博客网 时间:2024/04/28 03:51
工具:myeclipse 6.5
1.新建项目StrutsFileUpload
导入struts1.x的属性包:
在struts-config.xml里面新建一个action和form为fileupload
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTDStruts Configuration 1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans >
<form-bean name="fileuploadForm"type="com.shiryu.form.FileuploadForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="fileuploadForm"
input="/fileupload.jsp"
name="fileuploadForm"
path="/fileupload"
scope="request"
type="com.shiryu.action.FileuploadAction">
<forward name="index"path="/index.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.shiryu.ApplicationResources"/>
</struts-config>
2.com.shiryu.action包下的FileuploadAction.java
package com.shiryu.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.shiryu.form.FileuploadForm;
public class FileuploadAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequestrequest, HttpServletResponse response) {
FileuploadForm fileuploadForm =(FileuploadForm) form;
FormFileupfile=fileuploadForm.getFilecontext();
//文件上传功能实现
try {
byte[]buf=upfile.getFileData();
OutputStream out=new FileOutputStream(newFile("d://"+fileuploadForm.getFilecontext().toString()));
out.write(buf);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
returnmapping.findForward("index");
}
}
3.com.shiryu.form包下的FileuploadForm.java
package com.shiryu.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileuploadForm extends ActionForm {
private static final long serialVersionUID =5572854553277148095L;
//类型是FormFile
private FormFile filecontext;
private String filename;
public FormFile getFilecontext() {
return filecontext;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setFilecontext(FormFile filecontext) {
this.filecontext = filecontext;
}
}
fileUpload.jsp
<%@ pagelanguage="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean"prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html"prefix="html"%>
<html>
<head>
<title>JSP forFileuploadForm</title>
</head>
<body>
<html:form action="/fileupload"enctype="multipart/form-data">
filename :<html:text property="filename"/><html:errorsproperty="filename"/><br/>
filecontext :<html:file property="filecontext"/><html:errorsproperty="filecontext"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
- Struts文件上传功能的实现
- 基于struts 的FormFile实现文件的上传功能
- 基于struts 的FormFile实现文件的上传功能
- struts实现多个文件上传功能
- struts上传文件的实现
- 文件上传功能的实现
- 文件上传功能的实现
- 文件上传功能的实现
- 文件上传功能的实现
- 文件上传功能的实现
- struts--实现上传文件
- struts 实现文件上传
- struts 实现文件上传
- struts实现文件上传
- struts实现文件上传
- Struts实现文件上传。
- 用Struts实现动态多文件上传功能
- struts如何实现上传文件的重命名????
- 在Win32下,线程中的事件使用
- TEdit+TCombobox组合实现模糊匹配动态下拉
- C#的委托与Delphi中的"事件委托"
- tomca性能问题
- ewebEditor4.8默认上传文件大小限制到100k
- Struts文件上传功能的实现
- DoEvents的用法
- ADO.NET_DataReader对象
- 仅通过崩溃地址找出源代码的出错行
- new_modis gen_modis codegen_modis 三者的区别
- 父母也是北京人!
- Jsp+Servlet文件上传功能实现
- STM32 keil mdk启动代码发分析_转
- java web.xml详解