Skip to content

Commit

Permalink
Add upload file via byte[].
Browse files Browse the repository at this point in the history
  • Loading branch information
雷威 committed Oct 17, 2016
1 parent d5e2871 commit 14a7b69
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.leolei.dubbodemo.api;

import java.io.Serializable;

public interface ICar extends Serializable {
public String start();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.leolei.dubbodemo.api;

public interface ICarService {

public String start(String name,ICar car);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.leolei.dubbodemo.api;

public interface IFileService2 {
public void upload(String destPath, byte[] bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface IUserService {

User getUser(String name);

String getName(User user);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.leolei.dubbodemo.api;

public class User implements java.io.Serializable{
import java.io.Serializable;

public class User implements Serializable {
private String name;

public User(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.leolei.dubbodemo.consumer.controller;

import com.leolei.dubbodemo.api.ICar;

public class BMW implements ICar {

private final String name = "BMW";

@Override
public String start() {
return starting() + "......";
}

private String starting(){
StringBuffer sb = new StringBuffer();
sb.append(name).append("is starting");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@


import com.alibaba.dubbo.config.annotation.Reference;
import com.leolei.dubbodemo.api.IFileService;
import com.leolei.dubbodemo.api.IHelloService;
import com.leolei.dubbodemo.api.IUserService;
import com.leolei.dubbodemo.api.User;
import com.leolei.dubbodemo.api.*;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
Expand All @@ -22,49 +20,79 @@
@Controller
public class HelloController {

// @Autowired
// private IHelloService helloService;
//
// @Autowired
// private IUserService userService;
@Autowired
private IHelloService helloService;

@Autowired
private IUserService userService;

@Autowired
private IFileService2 fileService2;

@Autowired
private IFileService fileService;


// @RequestMapping(value = "/hello")
// public ModelAndView hello(){
//
// String name = helloService.sayHello("world");
//
// Map<String,Object> data = new HashMap<>();
// data.put("username",name);
//
// return new ModelAndView("user",data);
// }
//
// @RequestMapping(value = "/user")
// public ModelAndView user(){
//
// User user = userService.getUser("admin");
// String name = user.getName();
// Map<String,Object> data = new HashMap<>();
// data.put("username",name);
// return new ModelAndView("user",data);
// }
private ICarService carService;


@RequestMapping(value = "/hello")
public ModelAndView hello(){

String name = helloService.sayHello("world");

Map<String,Object> data = new HashMap<>();
data.put("username",name);

return new ModelAndView("user",data);
}

@RequestMapping(value = "/user")
public ModelAndView user(){
User user = new User("leiting");
String name = userService.getName(user);
Map<String,Object> data = new HashMap<>();
data.put("username",name);
return new ModelAndView("user",data);
}

@RequestMapping(value = "/test")
public ModelAndView test() throws FileNotFoundException {
public ModelAndView test() throws Exception {

InputStream stream = new FileInputStream("/users/leiwei/hello.txt");
InputStream stream = new FileInputStream("/users/leiwei/baidu.png");

//org.springframework.web.multipart.commons.CommonsMultipartFile file = new CommonsMultipartFile();
fileService.upload("1234.txt",stream);
//fileService.upload("1234.txt",stream);

byte[] bytes = readInputStream(stream);
fileService2.upload("1234.txt",bytes);

Map<String,Object> data = new HashMap<>();
data.put("username","");

return new ModelAndView("user",data);
}

@RequestMapping(value = "/test2")
public ModelAndView test2() throws Exception {

String s = carService.start("bmw",new BMW());

Map<String,Object> data = new HashMap<>();
data.put("username",s);

return new ModelAndView("user",data);
}



private static byte[] readInputStream(InputStream inputStream) throws Exception{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
return outputStream.toByteArray();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

<dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!--<dubbo:reference interface="com.leolei.dubbodemo.api.IHelloService" id="helloService" />-->
<dubbo:reference interface="com.leolei.dubbodemo.api.IHelloService" id="helloService" />

<!--<dubbo:reference interface="com.leolei.dubbodemo.api.IUserService" id="userService"/>-->
<dubbo:reference interface="com.leolei.dubbodemo.api.IUserService" id="userService"/>

<dubbo:reference interface="com.leolei.dubbodemo.api.IFileService" id="fileService" />

<dubbo:reference interface="com.leolei.dubbodemo.api.IFileService2" id="fileService2" />

<dubbo:reference interface="com.leolei.dubbodemo.api.ICarService" id="carService" />

<!--<dubbo:annotation/>-->
<!--<context:component-scan base-package="com.leolei.dubbodemo.consumer.controller">-->
<!--<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Reference"/>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.leolei.dubbodemo.provider;

import com.leolei.dubbodemo.api.ICar;
import com.leolei.dubbodemo.api.ICarService;

public class CarService implements ICarService {
@Override
public String start(String name, ICar car) {
String s = car.start();
return s + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.leolei.dubbodemo.provider;

import com.leolei.dubbodemo.api.IFileService2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileService2 implements IFileService2 {
@Override
public void upload(String destPath, byte[] bytes) {

try {
int b = 0;
//FileInputStream in = null;
FileOutputStream out = null;
try{
// 源文件路径
//in = new FileInputStream("D:\\text.txt");
// 结果文件路径
out = new FileOutputStream("/users/leiwei/baidu2.png");

for (int i =0; i < bytes.length;i++){
out.write(bytes[i]);
}

// while((b = stream.read()) != -1){
// out.write(b);
// }
//stream.close();
out.close();
}catch(FileNotFoundException e1){
System.out.println("file is not found!");
System.exit(-1);
}catch(IOException e2){
System.out.println("文件复制错误!!");
System.exit(-1);
}
System.out.println("文件复制成功了~~!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public class UserService implements IUserService {
public User getUser(String name) {
return new User(name);
}

@Override
public String getName(User user) {
return "Hello" + user.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@

<!--<dubbo:annotation package="com.leolei.dubbodemo.provider" />-->

<!--<bean id="helloService" class="com.leolei.dubbodemo.provider.HelloService"/>-->
<!--<dubbo:service interface="com.leolei.dubbodemo.api.IHelloService" ref="helloService"/>-->
<bean id="helloService" class="com.leolei.dubbodemo.provider.HelloService"/>
<dubbo:service interface="com.leolei.dubbodemo.api.IHelloService" ref="helloService"/>

<!--<bean id="userService" class="com.leolei.dubbodemo.provider.UserService" />-->
<!--<dubbo:service interface="com.leolei.dubbodemo.api.IUserService" ref="userService" />-->
<bean id="userService" class="com.leolei.dubbodemo.provider.UserService" />
<dubbo:service interface="com.leolei.dubbodemo.api.IUserService" ref="userService" />

<bean id="fileService" class="com.leolei.dubbodemo.provider.FileService" />
<dubbo:service interface="com.leolei.dubbodemo.api.IFileService" ref="fileService" />

<bean id="fileService2" class="com.leolei.dubbodemo.provider.FileService2" />
<dubbo:service interface="com.leolei.dubbodemo.api.IFileService2" ref="fileService2" />

<bean id="carService" class="com.leolei.dubbodemo.provider.CarService" />
<dubbo:service interface="com.leolei.dubbodemo.api.ICarService" ref="carService" />

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.leolei.dubbodemo.api;

import java.io.FileNotFoundException;
import java.io.InputStream;

public interface IFileService2 {
public void upload(InputStream stream,String destPath) throws FileNotFoundException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.leolei.dubbodemo.api;

public interface IFileService3 {
public void upload(String destPath, byte[] bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@


import com.alibaba.dubbo.config.annotation.Reference;
import com.leolei.dubbodemo.api.IFileService;
import com.leolei.dubbodemo.api.IHelloService;
import com.leolei.dubbodemo.api.IUserService;
import com.leolei.dubbodemo.api.User;
import com.leolei.dubbodemo.api.*;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
Expand All @@ -30,6 +28,12 @@ public class HelloController {
@Autowired
private IFileService fileService;

@Autowired
private IFileService2 fileService2;

@Autowired
private IFileService3 fileService3;

@RequestMapping(value = "/hello")
public ModelAndView hello() throws Exception {

Expand Down Expand Up @@ -58,16 +62,34 @@ public ModelAndView user(){


@RequestMapping(value = "/test")
public ModelAndView test() throws FileNotFoundException {
public ModelAndView test() throws Exception {

InputStream stream = new FileInputStream("/users/leiwei/baidu.png");
InputStream stream = new FileInputStream("/users/leiwei/hello.jpg");

//org.springframework.web.multipart.commons.CommonsMultipartFile file = new CommonsMultipartFile();
fileService.upload("1234.txt",stream);
//fileService.upload("1234.txt",stream);

//fileService2.upload(stream,"1234.txt");
byte[] bytes = readInputStream(stream);
fileService3.upload("1234.txt",bytes);
Map<String,Object> data = new HashMap<>();
data.put("username","");

return new ModelAndView("user",data);
}

private static byte[] readInputStream(InputStream inputStream) throws Exception{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
return outputStream.toByteArray();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<!--<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Reference"/>-->
<!--</context:component-scan>-->

<dubbo:reference interface="com.leolei.dubbodemo.api.IFileService2" id="fileService2" />
<dubbo:reference interface="com.leolei.dubbodemo.api.IFileService" id="fileService" />
<dubbo:reference interface="com.leolei.dubbodemo.api.IFileService3" id="fileService3" />

</beans>
Loading

0 comments on commit 14a7b69

Please sign in to comment.