/**
* 文件上传
* @author yangQiao
*
*/
@Component(value = "fileUploadAction")
@Scope(value = "prototype")
public class FileUploadAction extends ActionBase{
/**
*
*/
private static final long serialVersionUID = -5227897073465315133L;
private Logger log = Logger.getLogger(FileUploadAction.class);
@Resource(name = "stationLetterAttachmentService")
private IStationLetterAttachmentService stationLetterAttachmentService;
//上传文件
private File attach;
//文件类型
private String attachContentType;
//文件名称
private String attachFileName;
//保存路径
private String savePath;
//文件Id
private int attachId;
public String upload(){
FileInputStream input = null;
FileOutputStream output = null;
try{
if(log.isDebugEnabled()){
log.debug("开始上传文件," + DateUtil.currentTime());
}
input = new FileInputStream(getAttach());
output = new FileOutputStream(getSavePath() + "\\" + getAttachFileName());
byte[] bytes = new byte[1024];
int len;
while((len = input.read(bytes)) != -1){
output.write(bytes, 0, len);
}
//保存附件
StationLetterAttachment letterAttach = new StationLetterAttachment();
letterAttach.setFileName(getAttachFileName());
letterAttach.setFileSize(getAttach().length() + "kb");
letterAttach.setPath(getSavePath());
letterAttach.setUploadTime(DateUtil.currentDate());
setAttachId(stationLetterAttachmentService.addStationLetterAttachment(letterAttach));
if(log.isDebugEnabled()){
log.debug("文件上传结束," + DateUtil.currentTime());
}
}catch(Exception e){
log.info("文件上传失败," + e.getLocalizedMessage());
}finally{
close(input);
close(output);
}
return SUCCESS;
}
/**
* 获取文件保存路径
* @return
*/
public String getSavePath(){
return ServletActionContext.getServletContext().getRealPath(savePath == null ? "/upload" : savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getAttach() {
return attach;
}
public void setAttach(File attach){
this.attach = attach;
}
public String getAttachContentType() {
return attachContentType;
}
public void setAttachContentType(String attachContentType) {
this.attachContentType = attachContentType;
}
public void setAttachFileName(String attachFileName) {
this.attachFileName = attachFileName;
}
public String getAttachFileName() {
return attachFileName;
}
public int getAttachId() {
return attachId;
}
public void setAttachId(int attachId) {
this.attachId = attachId;
}
/**
* 关闭文件流
* @param output
*/
private void close(OutputStream output){
try {
output.close();
} catch (IOException e) {
log.info("关闭文件输出流异常," + e.getLocalizedMessage());
}
}
private void close(InputStream input){
try {
input.close();
} catch (IOException e) {
log.info("关闭文件输入流异常," + e.getLocalizedMessage());
}
}
}
//html文件
<div class="control-group">
<label class="control-label" for="uploadify">附件</label>
<div class="controls">
<input type="file" size="19" name="attach" id="uploadify"/>
</div>
</div>
//配置文件
<!-- json数据返回的Action配置 -->
<package name="json_package" namespace="/letter" extends="json-default">
<action name="upload" class="fileUploadAction" method="upload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">5000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="savePath">/upload</param>
<result name="success" type="json">
<param name="root">attachId</param>
</result>
</action>
</package>
这个在于前端使用了uploadify文件上次插件,然后增加了FileObjName的参数即可