List<File> upload;
//上传文件名集合
private List<String> uploadFileName;//上传的文件的自带名有后缀
private InputStream inputStream; // 下载用的
List<Fileform> addlist4;// 接收项目文件
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
文件上传的方法:
if (addlist4!=null) {
int j=0;
for (int i = 0; i < addlist4.size(); i++) {
Fileform file = addlist4.get(i);
if(file!=null){
String path = saveUploadFile(upload.get(j++));
String filename=this.getUploadFileName().get(i);
file.setFilename(filename);
file.setPath(path);
file.setOtherformid(project.getProjectid());
file.setFileforename(file.getFileforename());
file.setDeletestate(0);
fileformService.save(file);
}
}
}
/**
* 保存上传的文件,并返回文件在服务端的真实存储路径
*
* @param upload
* @return
*/
protected String saveUploadFile(File upload) {
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
// >> 获取路径
String basePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload_files");
String subPath = sdf.format(new Date());
// >> 如果文件夹不存在,就创建
File dir = new File(basePath + subPath);
if (!dir.exists()) {
dir.mkdirs(); // 递归的创建不存在的文件夹
}
// >> 拼接路径
String path = basePath + subPath + UUID.randomUUID().toString();
// >> 移动文件
upload.renameTo(new File(path)); // 如果目标文件夹不存在,或是目标文件已存在,就会不成功,返回false,但不报错。
return path;
}
下载的方法:
/** 下载 */
public String download() throws Exception {
// 准备下载的资源
Fileform fileform = fileformService.getById(model.getFileid());
inputStream = new FileInputStream(fileform.getPath());
// 准备文件名(解决乱码问题)
String fileName = URLEncoder.encode(fileform.getFilename(), "utf-8"); // 方法一
ActionContext.getContext().put("fileName", fileName);
return "download";
}
jsp:页面:
<script type="text/javascript">
var num4=0;
function addfile(){
var tr = document.createElement('tr');
var td = document.createElement('td');
td.innerHTML = "<td ><input type='text' name='addlist4["+num4+"].fileforename' required='required' id='filename'></td>";
tr.appendChild(td);
td = document.createElement('td');
td.innerHTML = "<td > <input type='file' name='upload' value='浏览' style='width:200px;'></td>";
tr.appendChild(td);
td = document.createElement('td');
td.innerHTML = "<td ><textarea rows='2' cols='20' required='required' name='addlist4["+num4+"].filedesc'></textarea></td>";
tr.appendChild(td);
td = document.createElement('td');
td.innerHTML = "<td> <input type='button' value='删除' onclick='removeSelect4(this)'/></td>";
tr.appendChild(td);
var dct = document.getElementById("table4");
dct.tBodies[0].appendChild(tr);
++num4;
}
function removeSelect4(inputobj){
var parentTD = inputobj.parentNode;
var parentTR = parentTD.parentNode;
var parentTBODY = parentTR.parentNode;
parentTBODY.removeChild(parentTR);
}
</script>
struts2的配置:
在特定的action中
<!-- 下载专用的结果配置 -->
<result name="download" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${#fileName}.doc"</param>
</result>