struts2 上传多个文件的问题

struts2 上传多个文件的问题,第1张

获得对应上传框中的文件数组,文件名数组和文件类型数组这个容易,你看下面代码就知道了import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.util.Date

import java.util.Iterator

import java.util.List

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpSession

import org.apache.commons.fileupload.FileItem

import org.apache.commons.fileupload.disk.DiskFileItemFactory

import org.apache.commons.fileupload.servlet.ServletFileUpload

import org.apache.struts2.interceptor.ServletRequestAware

import com.opensymphony.xwork2.Actionpublic class uploadfile implements Action,ServletRequestAware {

HttpServletRequest request

private File[] file

private String[] fileContentType

private String[] fileFileName

HttpServletRequest request

HttpSession sessionpublic void setServletRequest(HttpServletRequest request)

{this.request=request}

public File[] getFile() {

return file

}

public void setFile(File[] file) {

this.file = file

}

public String[] getFileContentType() {

return fileContentType

}

public void setFileContentType(String[] fileContentType) {

this.fileContentType = fileContentType

}

public String[] getFileFileName() {

return fileFileName

}

public void setFileFileName(String[] fileFileName) {

this.fileFileName = fileFileName

}*/ public String execute() throws Exception

{

session=request.getSession()

ConBean conbean=new ConBean()

conbean.con() //连接数据库

String name=(String)session.getAttribute("name")

for(int i=0i<file.lengthi++)

{

String address="resource/"+getFileFileName()[i]

String time=new Date().toString()

String sql="insert into resource(name,s_name,s_address,s_time) values('"+name+"','"+getFileFileName()[i]+"','"+address+"','"+time+"')"

conbean.insert(sql)

FileOutputStream w=new FileOutputStream("../webapps/webDemo/resource/"+getFileFileName()[i])

FileInputStream r= new FileInputStream(getFile()[i])

byte[] bt=new byte[19999999]

int len=0

if((len=r.read(bt))>0)

{

w.write(bt, 0, len)

}

w.close()

r.close()

}

return SUCCESS}

新建Web Project,在WebRoot下新建upload文件夹

在WebRoot下新建upload.jsp,上传界面

编写上传成功、失败的提示界面。

在WebRoot下新建uploadError.jsp

在WebRoot下新建 uploadSuccess.jsp

编写Action类

配置struts.xml文件,重置fileUpload拦截器。

测试,测试完成之后在tomcat下面webapps目录下找到项目对应的文件夹下的upload下查看

jsp代码

<s:form action="uploadAction!uploadFilemuch.action" method="post" enctype="multipart/form-data" >

File1:<s:file name="uploadmuch" label="selectfile"></s:file>

File2:<s:file name="uploadmuch" label="selectfile"></s:file>

File3:<s:file name="uploadmuch" label="selectfile"></s:file>

<s:submit value="submit" />

</s:form>

action代码

public class UpLoadFiled extends ActionSupport {

private File upload

private String uploadFileName

private File[] uploadmuch

private String[] uploadmuchFileName

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 File[] getUploadmuch() {

return uploadmuch

}

public void setUploadmuch(File[] uploadmuch) {

this.uploadmuch = uploadmuch

}

public String[] getUploadmuchFileName() {

return uploadmuchFileName

}

public void setUploadmuchFileName(String[] uploadmuchFileName) {

this.uploadmuchFileName = uploadmuchFileName

}

public String uploadFilemuch() throws IOException {

if (this.getUploadmuch() != null) {

for (int i = 0i <this.getUploadmuch().lengthi++) {

FileOutputStream fos = new FileOutputStream(

ServletActionContext.getRequest().getRealPath(

"\\upload")

+ "\\" + this.getUploadmuchFileName()[i])

FileInputStream fis = new FileInputStream(this.getUploadmuch()[i])

byte[] buffer = new byte[1024]

int len = 0

while ((len = fis.read(buffer)) >0) {

fos.write(buffer, 0, len)

}

}

}

ActionContext.getContext().put("message", "上传成功")

Log.addLog("上传成功")

return "message"

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7929231.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存