(原创) 使用AXIS 实现附件上传到服务器

(原创) 使用AXIS 实现附件上传到服务器

package com.telecom.communication;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.activation.DataHandler;

import org.apache.axis.attachments.ManagedMemoryDataSource;
import org.apache.axis.types.URI;
import org.apache.struts.upload.FormFile;

import cn.com.chinatelecom.www.schema.ctcc.common.v2_1.SimpleReference;
import cn.com.chinatelecom.www.schema.ctcc.multimedia_messaging.v2_2.MessagePriority;
import cn.com.chinatelecom.www.wsdl.ctcc.multimedia_messaging.send.v2_2._interface.SendMessage;
import cn.com.chinatelecom.www.wsdl.ctcc.multimedia_messaging.send.v2_2.service.SendMessageServiceLocator;

import com.telecom.dao.DBLinker;
import com.telecom.util.AttachPart;
import com.telecom.util.Sequence;

public class MMS_send {
 public MMS_send() {

 }

 private AttachPart attachmentParts[];

 private static SimpleReference _receipt_request;

   public void saveMmsMessage(String filepath, AttachPart[] ap,String mmsRoad) {
  for (int i = 0; i < ap.length; i++) {
   AttachPart part = ap[i];
   try {
    String s=saveAttachmentMessage(part.getContent().getInputStream(), part
      .getContentType(), mmsRoad, filepath);
    System.out.println("last s:"+s);

   } catch (Exception e) {

    e.printStackTrace();

   }
  }

 }

 public String saveAttachmentMessage(InputStream in, String contentType,
   String fileName, String folderPath) throws Exception {
  System.out.println("FileName: "+fileName);
  // 打开多媒体库
  File file = new File(folderPath + "/mmsImage");
  // 如果媒体库不存在则建设多媒体库
  if (!file.exists()) {
   file.mkdirs();
  }
  // 将图片存放到多媒体库,并保存其信息到数据库
  String filePath = file.getAbsolutePath();

  BufferedInputStream bin = null;
  OutputStream os = null;

  try {
   bin = new BufferedInputStream(in);
   file = new File(filePath + "/" + fileName);
   if (!file.exists()) {
    file.createNewFile();
   }
   os = new FileOutputStream(file);
   byte[] bytes = new byte[1];
   int beginIndex = 0;
   int endIndex = beginIndex + 1;
   while (true) {
    int _read = bin.read(bytes, beginIndex, endIndex);
    if (_read == -1) {
     break;
    }
    os.write(bytes);
   }
   String contextPathFileName = "mmsImage/" + fileName;
   bin.close();
   in.close();
   os.close();
   return contextPathFileName;
  } catch (IOException e) {
   e.printStackTrace();
   throw new Exception();
  } catch (Exception e) {
   e.printStackTrace();
   throw new Exception();
  } finally {
   try {
    bin.close();
    in.close();
    os.close();
   } catch (IOException e) {
    throw new Exception();
   }

  }
 }

 public AttachPart buildEMAttachmentPartFromFile(FormFile mmsFile) {
  AttachPart attchmentPart = new AttachPart();
  try {
   InputStream in = mmsFile.getInputStream();
   String contentType = mmsFile.getContentType();
   if ("image/pjpeg".equals(contentType))
    contentType = "image/jpeg";
   ManagedMemoryDataSource source = new ManagedMemoryDataSource(
     mmsFile.getInputStream(), 16384, contentType, true);
   attchmentPart.setContent(source);
   attchmentPart.setContentType(mmsFile.getContentType());
   String fileName = mmsFile.getFileName();
   String suffix = fileName.substring(fileName.indexOf('.'));
   attchmentPart.setFileName(Sequence.getID("") + suffix);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return attchmentPart;
 }

 public static void main(String[] args) {
  Sag_sms s = new Sag_sms();
  try {
   s.sendSmsBySAG("13977770119", "法律", "1004");
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
  }

 }

}

你可能感兴趣的:((原创) 使用AXIS 实现附件上传到服务器)