您的位置:首页 > 编程语言 > Java开发

myeclipse jaxws框架写 webservice 之上传图片方法

2013-07-15 11:16 204 查看
Jaxws 上传图片方法

获取图片名字 存至服务器

获取图片存至 服务器指定磁盘

这里定义三个参数 一个是图片名称(String 类型),用来存至服务器。二String类型的image

用在存取 经过Base64编码之后的的字节流。 参数三 是我数据库中得一个字段 用来确定 更新用户的行

下面是

Web服务器完整代码

我已经写好了服务器连接类

package cn.edu.bzu.operate;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;

import cn.edu.bzu.sqlconnection.sqlDatabaseConnection;

import sun.misc.BASE64Decoder;

public
class
userImageUpload extends sqlDatabaseConnection {
/**
* @authorxs
* @param imaName
* @param image
* @param Ptel
* @return boolean
*/

public
boolean
UpdateImage(String imaName,String image,String Ptel)
{
boolean SUCEESS=false;
int iResult=0;
FileOutputStream fos=null;
try {
getConnection();
StringsqlInsertUser="update personInfo set Pimage=? wherePtel=? ";
pStmt=conn.prepareStatement(sqlInsertUser);
pStmt.setString(1, imaName+".jpg");
pStmt.setString(2, Ptel);
iResult=pStmt.executeUpdate();
if(iResult>0)
{
byte[] buffer =newBASE64Decoder().decodeBuffer(image);
//System.out.println(buffer.length);
fos = new FileOutputStream("d:\\"+imaName+".jpg");
fos.write(buffer);
fos.close();
SUCEESS=true;
}
else{
SUCEESS=false;
}
}catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
closeAll();
try{
fos.flush();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
return SUCEESS;

}

经过发布之后

测试代码

Client

package cn.edu.bzu.upimage;
import java.io.*;

import com.sun.org.apache.xml.internal.security.utils.Base64;
public
class
testupImage {
public
static void
main(String[] args)throws IOException {
UserImageUploadDelegate service = newUserImageUploadService().getUserImageUploadPort();
FileInputStream fis =new FileInputStream("F:\\success.jpg");
try {

ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=newbyte[8192];
int count =0;
while((count =fis.read(buffer))>=0)
{
baos.write(buffer,0,count);
}
String uploadBuffer=new String(Base64.encode(baos.toByteArray()));
boolean Res=service.updateImage("success", uploadBuffer,"18899998765");
if(Res==true){
System.out.println("图片已经成功存到服务器");
}
else{
System.out.println("更新头像失败");
}
} catch (FileNotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}finally{
fis.close();
}

}

}

完成

以上是综合网上代码写

参考地址http://www.doc88.com/p-779458081779.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: