一个文件上传和下载的例子,思路:先上传几个文件,然后获取文件列表,列表中每个文件对应一个下载链接
文件上传:
Action:get/set方法省略
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File upload;
private String uploadFileName;
private String uploadContentType;
/*
* 文件上传
*/
@Override
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/upload");
FileOutputStream fos = new FileOutputStream(new File(path + "/" + getUploadFileName()));
FileInputStream inputStrem = new FileInputStream(getUpload());
byte[] byts = new byte[1024];
int temp = 0;
while ((temp = inputStrem.read(byts)) > 0) {
fos.write(byts, 0, te