三、补充点 jersey框架文件上传方式,包含带有参数上传[/b]
通过form表单提交方式进行图片上传
(一)、页面
XML/HTML code?
1
2
3
4
5
6
7
8
9
<
`div
class=
"header"
style=
"background-color:#14C2FF;width:400px,height:800px; border:1px red solid;"`>
<
`form
action=
"http://localhost:8080/ichanoDeveloper/product/uploadProductImg"
method=
"post"
enctype=
"multipart/form-data"`>
<
`p`>
<
`input
type=
"file"
class=
"sys_input"
name ="productLogo" id
=`"productLogo"
/>
剩productId:<
`input
type=
"text"
name=
"productId"
value=
"20"
id=
"productId"/><
br
/> `
</
`p`>
<
`input
type=
"submit"
value=
"上传"
/> `
</
`form`>
</
`div`>
(二)处理代码
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @Title: uploadFileCheck
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param fileInputStream
* @param @param disposition
* @param @param request
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
@POST
@Path
`("/uploadFileCheck"
)`
@Consumes
`(MediaType.MULTIPART_FORM_DATA)`
@Produces
`(MediaType.TEXT_PLAIN)`
public
Response uploadTemp(
`@FormDataParam(
"updateLicense")
final
InputStream uploadedInputStream,`
@FormDataParam
`("updateLicense"
) `final
FormDataContentDisposition fileDetail,
@FormDataParam
`( "surplusCount"
) final` `List<FormDataBodyPart> keywordObjs,
@Context
HttpServletRequest request ) {`
String fileName = Calendar.getInstance().getTimeInMillis()+ fileDetail.getFileName();
ZyUploadLicenseFileResponseVo zyUploadLicenseFileResponseVo =
`new
ZyUploadLicenseFileResponseVo();`
ZyUploadLicenseFileRequestVo zyUploadLicenseFileRequestVo =
`new
ZyUploadLicenseFileRequestVo();`
if
(keywordObjs !=
`null
&& ! keywordObjs.isEmpty()) {`
for
(FormDataBodyPart keywordObj : keywordObjs) {
String keyName = keywordObj.getName();
if
`("surplusCount"
.equals(keyName)){`
zyUploadLicenseFileRequestVo.setSurplusCount(keywordObj.getValueAs(String.
`class`));
break
`;`
}
}
}
String accessToken = TokenUtil.getAccessToken(request.getHeader(
`"auth_token"`),KeyUtil.getKey(context));
Key key= KeyUtil.getKey(context);
if
(TokenUtil.isValid(accessToken,key)){
zyUploadLicenseFileRequestVo.setInputStream(uploadedInputStream);
zyUploadLicenseFileRequestVo.setFileName(fileName);
zyUploadLicenseFileResponseVo= zyLicenseApiService.uploadBatchExcel(zyUploadLicenseFileRequestVo);
}
`else`{
zyUploadLicenseFileResponseVo.setCode(Constants.AUTH_RESULT_FILE_SESSIONOUT);
zyUploadLicenseFileResponseVo.setDesc(
`"Session is out!"`);
}
logger.info(
`"The responseJsonObject:"
+ JSONObject.toJSONString(zyUploadLicenseFileResponseVo));`
return
Response.status(
`200).entity(JSONObject.toJSONString(zyUploadLicenseFileResponseVo)).type(
new
MediaType(CommonStr.APPLICATION, CommonStr.JSON, CommonStr.UTF8)).build();`
}
@Override
public
ZyUploadLicenseFileResponseVo uploadBatchExcel(ZyUploadLicenseFileRequestVo zyUploadLicenseFileRequestVo) {
ZyUploadLicenseFileResponseVo zyUploadLicenseFileResponseVo =
`new
ZyUploadLicenseFileResponseVo();`
// 参数有误
if
(!zyUploadLicenseFileRequestVo.isValid()) {
logger.error(
`"The argument is null!"`);
zyUploadLicenseFileResponseVo.setCode(Constants.AUTH_RESULT_FILE_2001);
zyUploadLicenseFileResponseVo.setDesc(
`"The argument is invalid!!"`);
return
zyUploadLicenseFileResponseVo;
}
String fileName = zyUploadLicenseFileRequestVo.getFileName();
List licenseInfo =
`new
ArrayList<String>();`
if
`(!fileName.contains("xls"
)){`
logger.error(
`"The fileName[{}] is error!"`, fileName);
zyUploadLicenseFileResponseVo.setCode(Constants.UPLOAD_FAILE_9002);
zyUploadLicenseFileResponseVo.setDesc(
`"The file fomart is faile!!"`);
return
zyUploadLicenseFileResponseVo;
}
try
{
workbook =
`new
XSSFWorkbook(zyUploadLicenseFileRequestVo.getInputStream());`
XSSFSheet sheet = workbook.getSheetAt(
`0);
//获取第一个sheet`
int
rows = sheet.getPhysicalNumberOfRows();
`//行数`
int
cols = sheet.getRow(
`1).getPhysicalNumberOfCells();
//列数`
if
`(cols != 2
){`
logger.error(
`"The file fomart is faile!"`);
zyUploadLicenseFileResponseVo.setCode(Constants.UPLOAD_FAILE_9002);
zyUploadLicenseFileResponseVo.setDesc(
`"文件格式错误!!"`);
return
zyUploadLicenseFileResponseVo;
}
//校验上传的excel是否为标准模版
if
`(rows>0
){`
//有数据
for
`(int` `i=
3`;i<rows;i++){
//从第四行开始检索
String tempLicense = String.valueOf(sheet.getRow(i).getCell(
`1`));
if
`(tempLicense.length()<=32
){`
licenseInfo.add(tempLicense);
}
}
}
int
surplusLicenseInt = Integer.parseInt(zyUploadLicenseFileRequestVo.getSurplusCount());
zyUploadLicenseFileResponseVo.setCode(Constants.AUTH_RESULT_SUCCESS);
zyUploadLicenseFileResponseVo.setDesc(
`"Upload File Success!"`);
zyUploadLicenseFileResponseVo.createInstanceData().setLicenseList(licenseInfo);
return
zyUploadLicenseFileResponseVo;
}
`catch
(IOException e) {`
e.printStackTrace();
logger.error(
`"解析上传文件失败!"`);
zyUploadLicenseFileResponseVo.setCode(Constants.UPLOAD_FAILE_9002);
zyUploadLicenseFileResponseVo.setDesc(
`"解析上传文件失败!"`);
return
zyUploadLicenseFileResponseVo;
}
}
好,至此,项目已经可以跑起来。但是还没有进行集群部署,后面还要进行测试。有问题我会继续补上。
参考文献:
http://blog.csdn.net/li563868273/article/details/50277359
http://www.open-open.com/lib/view/open1433995002942.html
http://www.tuicool.com/articles/ZvA36nz
Original url: Access
Created at: 2020-09-02 17:59:13
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论