一般的项目都有独立的文件服务器,专门用于对文件的操作,从而减少主服务器的负载压力。例如对于存储图片的服务器来说,我们就可以使用jersey上传图片到图片服务器,项目中图片的加载就会从图片服务器上获取,从而不会影响主服务器的性能,例如京东商城就是典型的有独立的图片服务器。现在就用springMVC上传文件来介绍jersey是如何上传文件到图片服务器的。
1、创建一个springMVC的web项目导入springMVC所需jar包及必要的配置文件。在xml配置文件中加入springMVC支持文件上传的上传转换器bean
<!-- 上传转换器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize" value="1048576"/>
</bean>
2、添加jersey需要的jar包
3、编写springMVC 上传文件controller
@Controller
@RequestMapping("/upload")
public class UploadController {
//上传图片 通过@RequestParam(required = false) MultipartFile获取图片
@RequestMapping(value = "/uploadPic.do")
public void uploadBrandPic(@RequestParam(required = false) MultipartFile pic,HttpServletResponse response){
//图片名称生成策略---采用时间格式(精确到毫秒)并追加随机3位(10以内)数字
//精确到毫秒
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String picName = df.format(new Date());
//随机再生成3位 10以内的数
Random r = new Random();
for(int i=0;i<3;i++){
picName += r.nextInt(10);
}
//获取扩展名
String originalFilename = pic.getOriginalFilename();
String ext = FilenameUtils.getExtension(originalFilename);
//相对路径
String path = "upload/" + picName + "." + ext;
//全路径,该路径为后面创建的tomcat图片服务器的上传图片的web工程下存放图片的路径
String url = "http://localhost:8088/images-web/" + path;
//jersey 发送另一台Tomcat(可读写的)
Client client = new Client();
WebResource resource = client.resource(url);
try {
resource.put(String.class, pic.getBytes());
} catch (UniformInterfaceException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClientHandlerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONObject json = new JSONObject();
json.put("url", url);
json.put("path", path);
ResponseUtils.renderJson(response, json.toString());
}
4、jsp页面的上传form表单
<form action="/upload/uploadPic.do" method="post" enctype="multipart/form-data">
<input type="file" name="pic"/>
<input type="submit" value="Submit">
</form>
下面是配置jersey上传的图片服务器
5、再下载一个tomcat服务器,将其配置为图片服务器。tomcat默认是只读的不能向其中做写入操作,所以要将其修改为可读可写的。在tomcat的conf目录下找到web.xml文件,,在servlet中添加如下内容将tomcat修改为可读写的。
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
6、将该tomcat的端口修改为8088
<Connector port="8088" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8090" protocol="AJP/1.3" redirectPort="8443" />
7、在该图片服务器中加入编译后的web工程,创建工程方式为:在eclipse中创建一个名称为images-web的web工程,在该工程下创建一个文件夹upload用于保存图片,文件夹下任意创建一个文件防止打war丢失没有文件的文件夹。然后将该工程打成war包,最后将war放入图片服务器中作为接受上传图片的web工程。
8、启动该图片服务器,确保刚才的war包可以正常运行。
9、启动springmvc项目,进入jsp页面选择上传文件提交表单即可将图片上传到刚才配置的图片服务器中。