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
获取图片名字 存至服务器
获取图片存至 服务器指定磁盘
这里定义三个参数 一个是图片名称(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
相关文章推荐
- dwz框架项目 中调用uploadify方法上传图片
- jfinal框架ueditor上传图片的实现方法
- thinkPHP5框架整合plupload实现图片批量上传功能的方法
- c# 上传图片流,php端(laravel框架)接收处理方法
- CI(CodeIgniter)框架实现图片上传的方法
- Yii框架实现图片上传的方法详解
- jQuery实现判断上传图片类型和大小的方法示例
- Kindeditor(jsp)与SSH三大框架整合上传图片出错问题解决方案
- ThinKPHP框架+ajax实现图片无刷新上传
- Linux inode 耗尽导致图片/文件无法上传的解决方法
- ios使用http来上传图片实现方法
- Android 通过webservice上传多张图片到指定服务器详解
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- FF3(FireFox3),IE8上传图片实现图片预览的方法
- iOS 上传图片方法总结
- IOS 调用WebService 实现图片上传处理的解决办法
- codeigniter上传图片不能正确识别图片类型问题解决方法
- Python中暂存上传图片的方法
- 前后端分离之图片上传服务端处理方法
- php 异步上传图片几种方法总结