1. 程式人生 > >Hessian4.0.7+Spring3.2.2檔案上傳

Hessian4.0.7+Spring3.2.2檔案上傳

最近用Hessian4.0.7做檔案上傳,先給出自己做試驗的樣例程式碼,寫在tomcat7下,採用servlet3.0,配置程式碼如下:

**
 * 基於Servlet3.0的,相當於以前<b>web.xml</b>配置檔案的配置類
 * 
 * @author OneCoder
 * @Blog http://www.coderli.com
 * @date 2012-9-30 下午1:16:59
 */
publicclass DefaultWebApplicationInitializer implements
WebApplicationInitializer {

@Override
publicvoid onStartup(ServletContext appContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(DefaultAppConfig.class); appContext.addListener(new ContextLoaderListener(rootContext)); ServletRegistration
.Dynamic hessianServlet = appContext.addServlet("hessian", new HessianServlet()); hessianServlet.addMapping("/hessian"); hessianServlet.setInitParameter("service-class", HessianFileUploader.class.getName()); //ServletRegistration.Dynamic dispatcher = appContext.addServlet( //"dispatcher", new DispatcherServlet(rootContext));
//dispatcher.setLoadOnStartup(1); //dispatcher.addMapping("/"); //// Spring Security 過濾器配置 //FilterRegistration.Dynamic securityFilter = appContext.addFilter( //"springSecurityFilterChain", DelegatingFilterProxy.class); //securityFilter.addMappingForUrlPatterns(null, false, "/*"); }}

定義介面,並在服務端實現,Hessian4.0開始支援流作為引數傳遞,以前則採用byte[]方式傳遞:

public interface FileUploader {

void uplaodFile(String fileNameInputStream is);

}


public class HessianFileUploader implements FileUploader {

@Override
publicvoid uplaodFile(String fileNameInputStream is) {
try {
OutputStream out = new FileOutputStream("/Users/apple/Desktop/" + fileName);
int nLength = 0;
byte[] bData = newbyte[1024];
while (-1 != (nLength = is.read(bData))) {
out.write(bData, 0, nLength);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();}}
}

}

客戶端呼叫:

public class FileUploaderTest {
private FileUploader uploader;
private HessianProxyFactory factory = new HessianProxyFactory();
@Test publicvoid testFileUploader() throws FileNotFoundException, MalformedURLException {
uploader = (FileUploader) factory.create(FileUploader.class, "http://localhost:8080/onecoder-shurnim/hessian");
InputStream is = new FileInputStream("/Users/apple/git/onecoder-java/onecoder-shurnim/src/main/resources/logback.xml"); uploader.uplaodFile(is, "logback.xml"); }
 
}

很簡單方便。這裡曾遇到一個奇怪的問題,如果介面的引數順序調換,即InputStream在前則會報錯:

com.caucho.hessian.io.HessianProtocolException: uplaodFile: expected string at 0x3c (<)

Hessian協議沒有深入研究,不知道是不是一個約定或是要求。開發時需要注意。

如果整合spring,只需將servlet交由spring的代理裡即可,修改配置即可:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans>         
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>        
 <bean id="fileUploader" class="com.coderli.shurnim.file.HessianFileUploader"/>         
<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">                 
     <property name="service" ref="fileUploader"/> 
                <property name="serviceInterface" value="com.coderli.shurnim.file.FileUploader"/>        
 </bean> 
</beans>

親測spring3.2.2+Hessian4.0.7可用。檔案內容儲存完整。