前端之家收集整理的这篇文章主要介绍了
ajax异步上传图片webservice转存图片服务器,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
项目源代码及相关jar包:https://github.com/smallbug-vip/repo/tree/master/code/web/back
一、搭建SpringMVC环境
因为现在主要讨论ajax异步上传图片到图片服务器,所以配置SpringMVC环境就简略介绍一下了,更具体步骤可以Google或者直接从链接下载源码。如果不做修改直接运行源码时需要注意,该源码已经链接数据库了,所以在配置文件中需要修改jdbc.properties修改数据库链接参数。否则无法启动。
配置web.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID"version="2.5">
- display-name>back</
- context-paramparam-name>contextConfigLocationparam-value>classpath:application-context.xml
- listenerlistener-class>org.springframework.web.context.ContextLoaderListener<!--中文过滤器-->
- filterfilter-name>encodingfilter-class>org.springframework.web.filter.CharacterEncodingFilterinit-param>UTF-8filter-mappingurl-pattern>*.do>*.shtml<!--springmvc配置前台-->
- servletservlet-name>frontservlet-class>org.springframework.web.servlet.DispatcherServlet>classpath:springmvc-front.xmlservlet-mapping<!--springmvc配置后台-->
- >classpath:springmvc-back.xmlwelcome-file-listwelcome-file>index.html>index.htm>index.jspweb-app>
配置pom.xml
文件地址:
https://github.com/smallbug-vip/repo/blob/master/code/web/back/pom.xml
配置springmvc-back.xml
<!--springmvc扫包@Controller@Service.....-->
context:component-scanbase-package="cn.smallbug"
use-default-filters="false"context:include-filtertype="annotation"
expression="org.springframework.stereotype.Controller"/>
context:component-scan<!--jsp视图-->
beanid="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"propertyname="prefix"value="/"propertyname="suffix"value=".jsp"bean二、配置XML使SpringMVC可以上传图片
在springmvc-back.xml文件中添加:
<!--文件上传-->
beanid="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"propertyname="defaultEncoding"value="UTF-8">property<!--<propertyname="maxUploadSize"value="2348000"></property>-->
@H_27_
301@三、编写接收
文件的Controller
packagecn.smallbug.core.controller;
- import
java.io.IOException;
importjava.util.UUID;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.commons.io.FilenameUtils;
importorg.json.JSONObject;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.multipart.MultipartFile;
importcom.sun.jersey.api.client.Client;
importcom.sun.jersey.api.client.WebResource;
@Controller
publicclassUploadImage{
@RequestMapping(value="/upload/uploadImage.do")
voiduploadImage(@RequestParam(required=false)MultipartFilepic,HttpServletResponseresponse){
Stringext=FilenameUtils.getExtension(pic.getOriginalFilename());
Stringuuid=UUID.randomUUID().toString();
StringnewFileName=uuid+"."+ext;
Clientclient=newClient();
//另一台服务器的请求路径
Stringurl="http://192.168.88.131:8080/back/upload/"+newFileName;
//设置请求路径
WebResourceresource=client.resource(url);
//发送
try{
resource.put(String.class,pic.getBytes());
}catch(IOExceptione){
thrownewRuntimeException(e);
}
//将url格式化成json串
JSONObjectjo=newJSONObject();
jo.put("url",url);
response.setContentType("application/json;charset=UTF-8");
//response.setContentType("text/xml;charset=UTF-8");
//response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(jo.toString());
}