Struts2 文件上传

来源:互联网 发布:跳跃网络有没有版权 编辑:程序博客网 时间:2024/06/05 03:22

问题一   ajax能实现文件上传吗?

       不可以,ajax请求传的是字符,文件上传需要指定文件路径,所以不能。

      是否有js控件将文件从本地读取成io流 再上传给后台

      不可以,这样的js控件是不安全的,可以任意读取本地文件

     uploadAction 继承ActionSupport

    重写execute方法

jsp代码

<form id="img_form"    method="post"enctype="multipart/form-data"><input id="patientId" type="hidden" name="dto.patientImg.patientAccount.patientId" value="<%=request.getAttribute("patientId")%>"/><input id="fileToUpload" type="file" name="file" style="margin-left: 600px"onchange="preview(this)" accept="image/*" /></form>
js代码

function commitImg() {var file = $('#fileToUpload').val();if (file != null && file != "") {var form=$("#img_form");//进入上传的action的execute方法              form.attr("action",$WEB_ROOT_PATH+"/uploadImg/uploadImgCtrl.htm");form.submit();}}

action代码

@Namespace("/uploadImg")@Scope("prototype")@Action(value = "uploadImgCtrl",  results = {@Result(name = "success", type="redirectAction",params={"actionName","patientImgCtrl.htm"          ,"namespace","/patientImg"          ,"BLHMI","uploadSucc"          ,}) })@Blh("patientImgBlh")@JsonResults4Pojo({@JResult(BlhMethod = "saveImg", ognlExpress = "dto.patientImg")})@InterceptorRefs({@org.apache.struts2.convention.annotation.InterceptorRef("fileUploadStack"),   @org.apache.struts2.convention.annotation.InterceptorRef("dhccStack")})public class UploadImgAction extends ActionSupport {private static final long serialVersionUID = 1L;private PatientImgBlh patientImgBlh;private PatientImgDto dto = new PatientImgDto();private File file; //必须跟页面元素的name一样 **        private String fileContentType;//必须是**ContentType        private String fileFileName;//必须是**FileNamepublic String execute() throws Exception{try {FileInputStream fis = null;@SuppressWarnings("restriction")sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();if (dto.getPatientImg()!=null&&!"".equals(dto.getPatientImg())) {//byte[] strbyte = dto.getFalseimg().getBytes();@SuppressWarnings("restriction")//将base64编码的string 转换为byte[]byte[] strbyte = decoder.decodeBuffer(""); ;dto.getPatientImg().setElectrFile(strbyte);} if (file != null) {fis = new FileInputStream(file);byte[] buffer = new byte[fis.available()];fis.read(buffer);dto.getPatientImg().setElectrFile(buffer);//图片名称//    String[] str = fileFileName.split(".", 2);//    dto.getPatientImg().setFileName(str[0]);//    dto.getPatientImg().setFileType(str[1]);dto.getPatientImg().setFileName(fileFileName);dto.getPatientImg().setFileType(fileContentType);}try {patientImgBlh.save(dto);} catch (Exception e) {}if (fis != null) {// 关闭流fis.close();}} catch (Exception e) {e.printStackTrace();}return "success";}/** * @return the patientImgBlh */public PatientImgBlh getPatientImgBlh() {return patientImgBlh;}/** * @param patientImgBlh the patientImgBlh to set */public void setPatientImgBlh(PatientImgBlh patientImgBlh) {this.patientImgBlh = patientImgBlh;}/** * @return the dto */public PatientImgDto getDto() {return dto;}/** * @param dto the dto to set */public void setDto(PatientImgDto dto) {this.dto = dto;}/** * @return the file */public File getFile() {return file;}/** * @param file the file to set */public void setFile(File file) {this.file = file;}/** * @return the fileContentType */public String getFileContentType() {return fileContentType;}/** * @param fileContentType the fileContentType to set */public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;}/** * @return the fileFileName */public String getFileFileName() {return fileFileName;}/** * @param fileFileName the fileFileName to set */public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}}

补充一个图片预览的js代码

function preview(file) {var prevDiv = document.getElementById('preview');if (file.files && file.files[0]) {var reader = new FileReader();reader.onload = function(evt) {prevDiv.innerHTML = '<img style="height:228px;width:185px;margin-top:10px;margin-left:0px;" src="'+ evt.target.result + '" />';};reader.readAsDataURL(file.files[0]);} else {prevDiv.innerHTML = '<div align="middle" class="img" style="height:228px;width:185px;margin-top:10px;margin-left:0px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\''+ file.value + '\'"></div>';}}



表一 医疗技术表  表二 资料表 外键是技术标的ID

 表关系为一对多

如何在保存多方的时候 跟新一对多的关系

0 0
原创粉丝点击