Controller层:
package com.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tw.ecosystem.reindeer.web.HttpReqRes;
import tw.ecosystem.reindeer.web.Result;
import com.service.PortalService;
@RestController
public class Controller {
@Autowired
private PortalService portalService;
@RequestMapping(value = "/reciveAttach",method = RequestMethod.POST)
public String fileUpload(HttpServletRequest req, HttpServletResponse res) {
Result result = Result.getResult();
HttpReqRes httpReqRes = new HttpReqRes(req, res);
result = portalService.reciveAttach(httpReqRes);
res.setContentType("application/json; charset=UTF-8");
return result.toString();
}
}
Service 层:
package com.service;
import tw.ecosystem.reindeer.web.HttpReqRes;
import tw.ecosystem.reindeer.web.Result;
public interface PortalService {
/**
* 附件上传
* @param httpReqRes
* @return
*/
Result reciveAttach(HttpReqRes httpReqRes);
}
实现类:
package com.service.impl;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import reindeer.loginfo.LogInfoModuleService;
import tw.ecosystem.reindeer.web.HttpReqRes;
import tw.ecosystem.reindeer.web.Result;
import wfc.service.database.BlobHelper;
import wfc.service.database.Condition;
import wfc.service.database.Conditions;
import wfc.service.database.SQL;
import com.service.PortalService;
@Service
public class PortalServiceImpl implements PortalService {
@Override
public Result reciveAttach(HttpReqRes httpReqRes) {
Result result = Result.getResult();
MultipartHttpServletRequest params = ((MultipartHttpServletRequest) httpReqRes
.getRequest());
String sendId = params.getParameter("sendId");
String id = UUID.randomUUID().toString();
MultipartFile file = params.getFile("file");
if (file == null) {
return result.failed().setMsg("附件为空");
}
try {
byte[] sssByte;
String fileName = file.getOriginalFilename();
String sql = "insert into T_SEND_ATT(ST_ID,ST_SEND_ID,FILENAME) values(?,?,?)";
Object[] objects = new Object[] { id, sendId, fileName };
SQL.execute(sql, objects);
sssByte = file.getBytes();
Conditions conds = Conditions.newOrConditions();
conds.add(new Condition("ST_ID", Condition.OT_EQUAL, id));
BlobHelper.setBlob("T_SEND_ATT", "ATT_ADDITION",
conds.toString(), sssByte, conds.getObjectArray());
} catch (Exception e) {
e.printStackTrace();
}
return result.success().setMsg("上传成功");
}
}