一般的都是form里面加enctype="multipart/form-data这个参数,这就会导致后台只能接收二进制流,不能接收其他的参数了。。所以才用这个办法。
文件:
function read(){
var oFile = document.getElementById("myFile");
if(oFile.value == ""){
alert("请选择您要导入的文件");
return false;
}
var url = 'uploadFile.action?staffId='+operId+'&relaId='+relaId;
$.ajaxFileUpload
(
{
url:url,
secureuri:false,
fileElementId:'myFile',
dataType: 'xml',
success: function (data, status)
{
query();
alert("成功");
},
error: function (data, status, e)
{
alert(e);
}
}
)
return false;
}
顺序不能变的,因为是按照顺序加载的。 js/jquery-1.7.1.js 和js/jquery-1.7.2.min.js都可以自行百度下载
500000000
后台接收 Action文件
/**
* 文件上传类
* by:zwj
*/
public class UploadFileAction extends ActionSupport{
UploadFileService uploadFileService;
private File uploadFile; //得到上传的文件
private String uploadFileContentType; //得到文件的类型
private String uploadFileFileName; //得到文件的名称
private long staffId;
private long relaId;
public long getStaffId() {
return staffId;
}
public void setStaffId(long staffId) {
this.staffId = staffId;
}
public long getRelaId() {
return relaId;
}
public void setRelaId(long relaId) {
this.relaId = relaId;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public UploadFileService getUploadFileService() {
return uploadFileService;
}
public void setUploadFileService(UploadFileService uploadFileService) {
this.uploadFileService = uploadFileService;
}
public String uploadFile() {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
response.setCharacterEncoding("utf-8"); //务必,防止返回文件名是乱码
System.out.println("fileName:"+this.getUploadFileFileName());
System.out.println("contentType:"+this.getUploadFileContentType());
System.out.println("File:"+this.getUploadFile());
TyPrjFile prjFile = new TyPrjFile();
prjFile.setFileCon(uploadFile);
long idKey = commonService.queryIdkey();
prjFile.setIdKey(idKey);
prjFile.setStaffId(staffId);
prjFile.setRelaId(relaId);
prjFile.setUpDate(new Timestamp(System.currentTimeMillis()));
prjFile.setFileTp(0);
prjFile.setFileNm(this.getUploadFileFileName());
this.uploadFileService.insert(prjFile);
try {
response.getWriter().print("上传成功");
} catch (IOException e) {
e.printStackTrace();
}
return this.SUCCESS;
}
}
Service就不用写了,直接进入dao
public void insert(TyPrjFile jvi){
String sql="insert into ty_prj_file(ID_KEY,RELA_ID,FILE_NM,FILE_TP,bill_type,FILE_CON,staff_id,up_date)" +
"values("+jvi.getIdKey()+","+jvi.getRelaId()+",'"+jvi.getFileNm()+"',"+jvi.getFileTp()+","+jvi.getBillType()+",?,"+jvi.getStaffId()+",?)";
try {
this.insert(sql, jvi);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(String sql, final TyPrjFile pf) throws FileNotFoundException {
final InputStream blobIs = new FileInputStream(pf.getFileCon());
jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
lobCreator.setBlobAsBinaryStream(ps, 1, blobIs, (int) pf.getFileCon().length()); // File转Blob的
ps.setTimestamp(2, pf.getUpDate());
}
});
}
lobHandler 需要注意一下,得在applicationContext.xml文件里面配置
lazy-init="true">
下面Bean----TyPrjFile
public class TyPrjFile {
private int num;
private long idKey;
private long relaId;
private String fileNm;
private int fileTp;
private File fileCon;
private long billType;
private Timestamp upDate;
private long staffId;
// 自行getting setting
}
over -----