commons-fileupload-1.2.1.jar这个包是做啥用的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了commons-fileupload-1.2.1.jar这个包是做啥用的相关的知识,希望对你有一定的参考价值。

参考技术A 使用的主要jar包:commons-io-1.3.2.jar包;commons-fileupload-1.2.1.jar包;commons-lang-2.3.jar,在使用组件实现文件上传时候要注意前后台的一致性,注意在后台定义File文件对象名要与jsp页面中的file标签name名字一致,前台form标签属性一定要设置为enctype="multipart/form-data",而且传输方式必须是post方式。不然后台不认为提交的是文件类型,而认为是普通表单的提交。
废话不多说看主要实现代码:前台jsp代码:
<form id="file_upload_id" name="file_upload_name" action="$ctx /fileProcess/uploadTableFile" method="post" enctype="multipart/form-data">

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table_input">
<tr>
<td class="right">选择本地文件:</td><td colspan="5">
<input type="file" size="20" id="file" name="file"/>
<input type="hidden" name="tableid" value="<%=ids %>"/>
<input type="hidden" name="tumid" value="$param.dum" />
</td></tr>
</table>
<div class="clear_10px"></div>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table_button">
<tr><td align="left" class="btright"><input type="submit" value="上传" class="button_tj" onclick="getInfo()" /></td>
</tr>
</table>
</form>

后台Java代码:
import java.io.File;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;

public class FileProcessAction extends BaseAction

private FileDatumService filedatumService;
private AttachmentService attachmentService;
private AccountService accountservice;
private SystemService sysService;
private TDatumdetail detail;
private TDatum datum;
private Tablemanage tm;
public Integer id;

private File file;

private String fileContentType;

private String fileFileName;

private String savePath;

private String downloadFileName;
public String uploadTableFile()
response.setContentType("text/xml;charset=UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String fileName = "";

try
if (isMultipart)
fileName =getFileFileName();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String attaCode = sdf.format(new Date());
String suffix = FilenameUtils.getExtension(fileName);
String newName = attaCode.concat(".").concat(suffix);
String basepath = request.getRealPath("/");
System.out.println("ddd:"+getSavePath());
// 保存文件
FileUtils.copyFile(getFile(), new File(basepath+getSavePath()+"/"+newName));
// 保存数据库
Attachment atta = new Attachment();
atta.setAttaName(fileName);
atta.setCurrentName(newName);
atta.setUploadTime(new Date());
atta.setUrl("/upload/" + newName);
int id=Integer.valueOf(request.getParameter("tableid"));
Tablemanage tabma=new Tablemanage();
tabma.setTablefile(fileName);
accountservice.tableUpdateSingle(id,tabma);
id=attachmentService.addFileattachment(atta);
session.put("fileid", id);
session.put("uploadFileName", newName);
return SUCCESS;

catch (Exception e)
e.printStackTrace();

return "failure";

public AttachmentService getAttachmentService()
return attachmentService;


public void setAttachmentService(AttachmentService attachmentService)
this.attachmentService = attachmentService;


public File getFile()
return file;


public void setFile(File file)
this.file = file;


public TDatumdetail getDetail()
return detail;

public void setDetail(TDatumdetail detail)
this.detail = detail;

public Integer getId()
return id;

public void setId(Integer id)
this.id = id;

public String getFileContentType()
return fileContentType;


public void setFileContentType(String fileContentType)
this.fileContentType = fileContentType;


public String getFileFileName()
return fileFileName;


public void setFileFileName(String fileFileName)
this.fileFileName = fileFileName;


public String getSavePath()
return savePath;


public void setSavePath(String savePath)
this.savePath = savePath;


public void setDownloadFileName(String downloadFileName)
this.downloadFileName = downloadFileName;


public String getDownloadFileName()
return downloadFileName;

public FileDatumService getFiledatumService()
return filedatumService;

public void setFiledatumService(FileDatumService filedatumService)
this.filedatumService = filedatumService;

public AccountService getAccountservice()
return accountservice;


public void setAccountservice(AccountService accountservice)
this.accountservice = accountservice;

public SystemService getSysService()
return sysService;

public void setSysService(SystemService sysService)
this.sysService = sysService;

public TDatum getDatum()
return datum;

public void setDatum(TDatum datum)
this.datum = datum;



在truts.xml里面的action中可以配置要上传文件的路径
<action name="uploadTableFile" class="com.sunway.storage.action.FileProcessAction"
method="uploadTableFile">
<param name="savePath">/upload</param>

</action>

以上是关于commons-fileupload-1.2.1.jar这个包是做啥用的的主要内容,如果未能解决你的问题,请参考以下文章

commons-fileupload-1.2.1.jar这个包是做啥用的

struts2的文件上传机制

struts2学习笔记之十:文件上传

Struts

Struts

Struts