用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv

用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv,第1张

package cn.itcast.struts2.demo1

import java.io.File

import org.apache.commons.io.FileUtils

import org.apache.struts2.ServletActionContext

import com.opensymphony.xwork2.ActionSupport

/**

* 完成文件上传 (不是解析上传内容,因为上传内容 由fileUpload拦截器负责解析)

*

* @author seawind

*

*/

public class UploadAction extends ActionSupport {

// 接收上传内容

// <input type="file" name="upload" />

private File upload// 这里变量名 和 页面表单元素 name 属性一致

private String uploadContentType

private String uploadFileName

public void setUpload(File upload) {

this.upload = upload

}

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType

}

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName

}

@Override

public String execute() throws Exception {

if (upload == null) { // 通过xml配置 required校验器 完成校验

// 没有上传文件

return NONE

}

// 将上传文件 保存到服务器端

// 源文件 upload

// 目标文件

File destFile = new File(ServletActionContext.getServletContext()

.getRealPath("/upload") + "/" + uploadFileName)

// 文件复制 使用commons-io包 提供 工具类

FileUtils.copyFile(upload, destFile)

return NONE

}

}

文件上传

package cn.itcast.struts2.demo1

import java.io.File

import org.apache.commons.io.FileUtils

import org.apache.struts2.ServletActionContext

import com.opensymphony.xwork2.ActionSupport

/**

* 支持多文件上传

*

* @author seawind

*

*/

public class MultiUploadAction extends ActionSupport {

// 接收多文件上传参数,提供数组接收就可以了

private File[] upload

private String[] uploadContentType

private String[] uploadFileName

public void setUpload(File[] upload) {

this.upload = upload

}

public void setUploadContentType(String[] uploadContentType) {

this.uploadContentType = uploadContentType

}

public void setUploadFileName(String[] uploadFileName) {

this.uploadFileName = uploadFileName

}

@Override

public String execute() throws Exception {

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

// 循环完成上传

File srcFile = upload[i]

String filename = uploadFileName[i]

// 定义目标文件

File destFile = new File(ServletActionContext.getServletContext()

.getRealPath("/upload" + "/" + filename))

FileUtils.copyFile(srcFile, destFile)

}

return NONE

}

}

/**

上传文件

*/

public class FileAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

try {

FileForm fileform = (FileForm) form

//取得请求的文件集合

Hashtable hash = fileform.getMultipartRequestHandler().getFileElements()

//得到hashtable的枚举值

Enumeration enu = hash.elements()

//如果该枚举值包含有其它的文件

while(enu.hasMoreElements()) {

//得到文件

FormFile file = (FormFile) enu.nextElement()

System.out.println(file)

add(file)

}

return mapping.findForward("yes")

} catch (Exception e) {

e.printStackTrace()

}

return super.execute(mapping, form, request, response)

}

public void add(FormFile file){

try {

//取得写文件的目录

String url=servlet.getServletContext().getRealPath("upload")

File f1=new File(url)

if(!f1.exists()){//如果文件目录不存在

f1.mkdirs()//创建目录

}

String fileName=file.getFileName()

//创建一个文件输入流

InputStream is=file.getInputStream()

OutputStream out=new FileOutputStream(url+"/"+fileName)

int byteRead=0

byte[] by=new byte[8192]

while((byteRead=is.read(by, 0, 8192))!=-1){

out.write(by, 0, byteRead)

}

out.close()

is.close()

file.destroy()

} catch (Exception e) {

e.printStackTrace()

}

}

}

/**

下载文件

*/

页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中

Action代码如下:

ArrayList list = new ArrayList()

String path=request.getRealPath("/")+"file"

String FullPath

//System.out.println(path)

myDir=new File(path)

list.clear()

contents=myDir.listFiles()

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

FullPath=contents.getName()

list.add(FullPath)

//System.out.println(FullPath)

}

request.setAttribute("list",list)

ActionForward forward=new ActionForward("/download.jsp")

return forward

然后进入download.jsp中,这个页面主要负责把所有文件显示,并提供下载连接,代码如下:

<%@ page language="java" contentType="text/htmlcharset=GBK" import="java.util.ArrayList"%>

<head>

</style>

</head>

<body>

<%ArrayList list=(ArrayList)request.getAttribute("list")

for(int i=0i<list.size()i++)

{

String a=java.net.URLEncoder.encode((String)list.get(i))

out.print("<a href=./loaded.do?name="+a+">"+list.get(i)+"</a><br>")

}

%>

</body>

</html>

注意,下划线画中的代码的作用,就是解决问题的所在。

接下来可以直接传入到loadedaction中,也可以通过一个form,我演示的是通过一个form

Form代码如下

package org.aeolus.struts.form

import javax.servlet.http.HttpServletRequest

import org.apache.struts.action.ActionErrors

import org.apache.struts.action.ActionForm

import org.apache.struts.action.ActionMapping

public class LoadForm extends ActionForm {

/*

*Generated Methods

*/

private String name

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

}

接下来就是action的代码

LoadForm doc=(LoadForm)form

String docName = new String(doc.getName().getBytes("8859_1"))

File f

if(docName!=""){

String docFullPath=request.getRealPath("/")

f = new File(docFullPath+"file\\"+docName)

response.reset()

response.setContentType("application/x-msdownloadcharset=GBK")

System.out.print(response.getContentType())

response.setCharacterEncoding("UTF-8")

docName=java.net.URLEncoder.encode(docName,"UTF-8")

response.setHeader("Content-Disposition", "attachmentfilename=" +new String(docName.getBytes("UTF-8"),"GBK"))

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f))

byte[] buf = new byte[1024]

int len = 0

OutputStream out = response.getOutputStream()

while((len = br.read(buf)) >0)

out.write(buf,0,len)

out.close()

response.wait()

ActionForward forward=new ActionForward("/download.jsp")

return forward }

return null

注意,下划线画中的代码的作用,就是解决问题的所在。说明一下:

response.setCharacterEncoding("UTF-8")

docName=java.net.URLEncoder.encode(docName,"UTF-8")

response.setHeader("Content-Disposition", "attachmentfilename=" +new String(docName.getBytes("UTF-8"),"GBK"))

如果不这样做你将要下载的文件名是乱码。


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存