ajax文件上传
发布时间:2020-12-15 21:57:24 所属栏目:百科 来源:网络整理
导读:后台代码package com.hitdy.dyda.action;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.text.ParseException;import javax.servlet.http.HttpServletResponse;import org.apache.comm
后台代码 package com.hitdy.dyda.action; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.text.ParseException; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.hitdy.dyda.service.IFileUploadService; import com.opensymphony.xwork2.ActionContext; public class FileUploadAction { //上传用 private File pic; private String picFileName; private String picContentType; private InputStream inputStream; private String gys; private String data=""; private IFileUploadService fileUploadService; /** * 功能:向前台返回的数据 * @author 闫加盼 * @return null * @param data 要向前台写入的对象 */ public void wirterdata(String data){ PrintWriter out = null; try { HttpServletResponse response = ServletActionContext.getResponse(); out = response.getWriter(); out.print(data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ out.flush(); out.close(); } } /** * 功能:判断是否是分析对象 * @author 闫加盼 * @return null * @throws IOException * @throws ParseException */ public void isAnalysisobject(){ String selfnum = picFileName.substring(0,11); String isAnalysisobject = this.fileUploadService.getIsAnalysisobject(selfnum); System.out.println(isAnalysisobject); if("yes".equals(isAnalysisobject)){ //是分析对象可以上传 this.upload(); }else{ //不是分析对象 this.wirterdata("<script>parent.returnback('no');</script>"); } } /** * 功能:文件上传 * @author 闫加盼 * @return null * @throws IOException * @throws ParseException */ public void upload(){ String isok; String errordata = "<script>parent.callback('no');</script>"; String successdata = "<script>parent.callback('yes');</script>"; try { /* //测试基站数据导入,和归属地数据导入,图片动用 try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ //服务器的路径 String realpath = ServletActionContext.getServletContext().getRealPath("/shuju"); //例如-------D:apache-tomcat-6.0.18webappsstruts2_uploadshuju String lj = realpath+""+picFileName; //列如------ D:apache-tomcat-7.0.47webappsdydashuju15636139999[通话详单]查询_1_1.xls if (pic != null) { File savefile = new File(new File(realpath),picFileName); if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); FileUtils.copyFile(pic,savefile); ActionContext.getContext().put("message","文件上传成功"); } System.out.println("lj:=="+lj); if("lt".equals(gys)){ //联通数据导入 isok = this.fileUploadService.daorult(lj,picFileName); if("no".equals(isok)){ this.wirterdata(errordata); }else{ this.wirterdata(successdata); } }else if("yd".equals(gys)){ //移动数据导入 isok = this.fileUploadService.daoruyd(lj,picFileName); if("no".equals(isok)){ this.wirterdata(errordata); }else{ this.wirterdata(successdata); } }else if("gsd".equals(gys)){ //归属地的数据导入 isok = this.fileUploadService.daorugsd(lj); if("no".equals(isok)){ this.wirterdata(errordata); }else{ this.wirterdata(successdata); } }else if("jz".equals(gys)){ //基站数据导入到数据库 isok = this.fileUploadService.jzUpload(lj); if("no".equals(isok)){ this.wirterdata(errordata); }else{ this.wirterdata(successdata); } }else{ //电信数据导入 isok = this.fileUploadService.daorudx(lj); if("no".equals(isok)){ this.wirterdata(errordata); }else{ this.wirterdata(successdata); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public File getPic() { return pic; } public void setPic(File pic) { this.pic = pic; } public String getPicFileName() { return picFileName; } public void setPicFileName(String picFileName) { this.picFileName = picFileName; } public String getPicContentType() { return picContentType; } public void setPicContentType(String picContentType) { this.picContentType = picContentType; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getGys() { return gys; } public void setGys(String gys) { this.gys = gys; } public String getData() { return data; } public void setData(String data) { this.data = data; } public IFileUploadService getFileUploadService() { return fileUploadService; } public void setFileUploadService(IFileUploadService fileUploadService) { this.fileUploadService = fileUploadService; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |