服务器端java struts2的phonegap上传

前端:

         //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的变量名+FileName
	private 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;
	 }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值