最近需要为移动端写接受并处理file的接口,查了查资料,最后把结果总结一下,本人用的是ssm框架哦~,分为文件服务器(文件接收处理的服务器,提供接口),客户机(发送文件,等待文件服务器处理)
文件服务器
1、需要引入上传组件的jar包,在pom.xml中加入:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2、在spring的配置文件中加入bean注入:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
3、web.xml中配置文件长传限制
<multipart-config>
<!-- <location>/tmp</location> -->
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
4、加入拦截器或者过滤器对接口访问请求进行过滤,我用的是springMVC的拦截器(因为所有参数都需要加解密,将加解密过程加到了拦截器中):
拦截器MVC文件配置:
<mvc:interceptors>
<!-- <bean class="com.loan.fore.shiro.filter.LoanInterceptor"/> -->
<!-- 对特定的url拦截 -->
<mvc:interceptor>
<!-- /**的意思是所有文件夹及里面的子文件夹
/*是所有文件夹,不含子文件夹
/是web项目的根目录 -->
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/static/*"/>
<mvc:exclude-mapping path="/errorRequest"/>
<bean class="com.loan.fore.shiro.filter.LoanInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
拦截器java
package com.loan.fore.shiro.filter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.loan.fore.util.ReturnEntityUtils;
import com.loan.fore.util.ThreeDESUtil;
public class LoanInterceptor implements HandlerInterceptor{
//从配置文件中获取项目运行模式(开发、测试、生产)具体参考:http://blog.csdn.net/dreamer_8399/article/details/75230443
@Value("${isProduct}")
private String isProduct;
@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView model)
throws Exception {
Map<String, Object> map2 = new HashMap<>();
if(req.getAttribute("returnMap")!=null){
Map<String, Object> map = (Map<String, Object>) req.getAttribute("returnMap");
//如果是生产模式的话,对返回map进行加密
if(isProduct.equals("true")){
try {
String resCiphertext = ThreeDESUtil.encryptThreeDESECB(JSON.toJSONString(map), ThreeDESUtil.KEY);
map2.put("dec", resCiphertext);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resp.getWriter().write(JSON.toJSONString(ReturnEntityUtils.SUCCESS_RETURN.put(map2)));
}
//其他情况直接返回map,方便开发人员调试
else{
resp.getWriter().write(JSON.toJSONString(ReturnEntityUtils.SUCCESS_RETURN.put(map)));
}
}
else{
map2.put("dec", "");
resp.getWriter().write(JSON.toJSONString(ReturnEntityUtils.SUCCESS_RETURN.put(map2)));
}
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
// 获得在下面代码中要用的request,response,session对象
String ciphertext = "";
if (req.getParameter("ciphertext") != null) {
ciphertext = req.getParameter("ciphertext").toString();
}
String res = ThreeDESUtil.decryptThreeDESECB(ciphertext, ThreeDESUtil.KEY);
resp.setContentType("application/json;charset=utf-8");
String path = req.getRequestURI();
//把解密数据setAttribute
req.setAttribute("ciphertext", res);
//如果是生产模式的话,对请求进行拦截
if(isProduct.equals("true")){
String xEquipment = req.getHeader("X-Equipment");
if (xEquipment != null) {
if (xEquipment.indexOf("/与移动端约定标识") > 0) {
return true;
} else {
resp.sendRedirect("/creditAPP/errorRequest");
return false;
}
} else {
resp.sendRedirect("/creditAPP/errorRequest");
return false;
}
}
//如果是开发、test模式的话,比进行拦截,方便开发人员进行测试
else{
return true;
}
}
}
5、接口,处理客户机传过来的文件
方式一:
package com.loan.fore.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import com.loan.fore.bind.method.IsShowDTO;
/**
* @ClassName: UeditorController
* @Descrip