用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

}

}

下载代码:

这里我使用的是SpringMVC,不过它在这里的唯一用途就是用来获取ServletContext对象,这个对象的用途,下面实例中有说明

下载,需要用到两个jar包:commons-fileupload.jar和commons-io.jar

Java代码

import org.springframework.stereotype.Controller

import org.springframework.web.bind.annotation.RequestMapping

import org.springframework.web.context.ServletContextAware

import javax.servlet.ServletContext

import javax.servlet.ServletOutputStream

import javax.servlet.http.HttpServletResponse

import java.io.*

@Controller

public class FileController implements ServletContextAware{

//Spring这里是通过实现ServletContextAware接口来注入ServletContext对象

private ServletContext servletContext

@RequestMapping("file/download")

public void fileDownload(HttpServletResponse response){

//获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载

String path = servletContext.getRealPath("/")

//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型

response.setContentType("multipart/form-data")

//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)

response.setHeader("Content-Disposition", "attachmentfileName="+"a.pdf")

ServletOutputStream out

//通过文件路径获得File对象(假如此路径中有一个download.pdf文件)

File file = new File(path + "download/" + "download.pdf")

try {

FileInputStream inputStream = new FileInputStream(file)

//3.通过response获取ServletOutputStream对象(out)

out = response.getOutputStream()

int b = 0

byte[] buffer = new byte[512]

while (b != -1){

b = inputStream.read(buffer)

//4.写到输出流(out)中

out.write(buffer,0,b)

}

inputStream.close()

out.close()

out.flush()

} catch (IOException e) {

e.printStackTrace()

}

}

@Override

public void setServletContext(ServletContext servletContext) {

this.servletContext = servletContext

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存