日期:2014-05-17  浏览次数:20612 次

文件上传和下载问题,各位大侠给点意见
问题首先描述下:
  
  是这样的,供应商在提出申请的时候,需要上传相应的资料,这里支持.doc、.xls格式的文件,待审核员审核时,需要下载

来审核供应商上传的资料,这里只有一个下载附件(附件中包括供应商提供的所有相关资料)的按钮?这代码是怎么实现的?

疑问:附件在服务器上的存放路径是怎样的?如果几个申请的附件名称一致应该如何处理?


这里说明下,因为以前没怎么写过上传下载文件的代码,这对我来说可能就是个艰难挑战了,求各位大侠支招!感激不尽!

------解决方案--------------------
文件名一致的问题,把上传的文件后解析出来后加上上传的时间,或者供应商的编号都可以的。反更有唯一就行了
------解决方案--------------------
用uploadify上传文件
------解决方案--------------------
可以通过配置文件设置上传到服务器的目录,就象前面的大侠说的,不要设D盘,C盘什么的;设好文件标题,文件类型,文件名称。
一下是一个Struts2的小例子:
页面:
<form action="fileupload.action" method="post" enctype="multipart/form-data">
aa 文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
Struts2:
package com.test.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.opensymphony.xwork2.ActionSupport;
import com.test.util.Parameter;

public class FileUploadAction extends ActionSupport {
/**
* 设置默认的serialVersionUID
*/
private static final long serialVersionUID = 1L;

// 页面文件标题
private String title;
// 上传文件流
private File upload;
// struct2上传文件类型
private String uploadContextType;
// 上传文件名称
private String uploadFileName;

// private String savePath;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadContextType() {
return uploadContextType;
}

public void setUploadContextType(String uploadContextType) {
this.uploadContextType = uploadContextType;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

// 通过struts2的配置文件得到上传目录,这个是很重要的
// @SuppressWarnings("deprecation")
// public String getSavePath() {
// return ServletActionContext.getRequest().getRealPath(savePath);
// }
//
// public void setSavePath(String value) {
// this.savePath = value;
// }

@Override
public String execute() throws Exception {
System.out.println("d:/flexuploadfile/");
System.out.println(getUploadFileName());
// 时间操作类
Calendar cal = Calendar.getInstance();
SimpleDateFormat smdf = new SimpleDateFormat("yyyyMMddhhmmss");
FileOutputStream fos = new FileOutputStream("d:/flexuploadfile/"
+ smdf.format(cal.getTime()) + "-" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
return "showUpload";
}

}
------解决方案--------------------
一个文本框怎么上传多个文件哦,你可以上传的时候只显示一个,当点击浏览后如果想再上传多个文件,点击之后下面增加一行,也就是再弹一个上传文件的框出来,你可以看一下ITEYE上面写文章的时候上传文件就是这样实现的。