服务器端java struts2的phonegap上传

来源:互联网 发布:淘宝地址转换 编辑:程序博客网 时间:2024/05/22 13:50

前端:

         //voiceURI 所要上传的文件本地URI            var options = new FileUploadOptions();             options.fileKey="upload";             options.fileName=voiceURI.substr(voiceURI.lastIndexOf('/')+1);             console.log(options.fileName+" ~~~  "+voiceURI);            options.mimeType="multipart/form-data";//设置为这种类型,则和form表单提交一样            options.chunkedMode = false; //必须 不知道什么原因                        /**var params = new Object();              params.value1 = "test";              params.value2 = "param";                            options.params = params;  */                        var ft = new FileTransfer();     //RecordVoice.uploadAction    struts2 action            ft.upload(voiceURI, RecordVoice.uploadAction, function(r){                        console.log("Code = " + r.responseCode);                        console.log("Response = " + r.response);                         console.log("Sent = " + r.bytesSent);                         console.log("voiceNetPath = " + voiceNetPath);                                             }, function(error){                        alert("An error has occurred: Code = " = error.code);                      }, options);



服务器端:

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport{ private String title; private File upload; //File变量名 必须与options.fileKey的值相同 private String uploadContentType;//变量名必须 File的变量名+ContentType private String uploadFileName;//变量名必须 File的变量名+FileNameprivate String allowTypes; // 接受依赖注入的属性 private String savePath; // 接受依赖注入的方法 public void setSavePath(String value) { this.savePath = value; } private String getSavePath(){//  return ServletActionContext.getServletContext().getRealPath(savePath); return "D:\\apache-tomcat-6.0.29\\webapps\\ROOT\\HIMSSVOICE"; } public void setTitle(String title) {  this.title = title; } public void setUpload(File upload) {  this.upload = upload; } public void setUploadContentType(String uploadContentType) {  this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) {  this.uploadFileName = uploadFileName; } public String getTitle() {  return (this.title); } public File getUpload() {  return (this.upload); } public String getUploadContentType() {  return (this.uploadContentType); } public String getUploadFileName() {  return (this.uploadFileName); } @Override public String execute()  {  System.out.println("开始上传单个文件---");  System.out.println("地-址:"+getSavePath());  System.out.println("==========" + getUploadFileName());  System.out.println("==========" + getUploadContentType());  System.out.println("==========" + getUpload());  System.out.println("=====0====="+allowTypes);  // 判断是否允许上传//  String filterResult = filterType(this.getAllowTypes().split(","));//  if (filterResult != null) {//   ActionContext.getContext().put("typeError","您要上传的文件类型不正确");//   return filterResult;//  }  System.out.println("=====1=====");  // 以服务器的文件保存地址和原文件名建立上传文件输出流  FileOutputStream fos;try { File savefile = new File(getSavePath() + "\\"+ getUploadFileName()); if (!savefile.getParentFile().exists()){           savefile.getParentFile().mkdirs(); } fos = new FileOutputStream(getSavePath() + "\\"    + getUploadFileName()); FileInputStream fis = new FileInputStream(getUpload());  byte[] buffer = new byte[1024];  int len = 0;  while ((len = fis.read(buffer)) > 0) {   fos.write(buffer, 0, len);  }} catch (Exception e) {e.printStackTrace();}  return SUCCESS;   }  public String filterType(String[] types) {  String fileType = this.getUploadContentType();  for (String type : types) {   if (type.equals(fileType)) {    return null;   }  }  return INPUT; } public String getAllowTypes() {  return allowTypes; } public void setAllowTypes(String allowTypes) {  this.allowTypes = allowTypes; }}





原创粉丝点击