android 开发中图片上传是很正常的,有两种可用的方式:
下面我们就说明一下以文件流上传图片的方式, 实现网络框架是Retrofit
测试上传3张手机sd卡中的图片,并传人了参数EquipmentCode, Description, ReportUserCode等
其中的思路是: Post的方式,Content-Type:multipart/form-data的类型进行上传文件的。
其中MultipartBody是RequestBody的扩展,
看看请求头的信息, 请求中携带了所有信(如果接口开发人员说不能收到, 叫他自己想想,截图给他,哈哈哈:)
上面的是上传了3张图片,如果一张,只要传一个就行!
就这样,图片上传的两种方式ok拉,测试通过的,保证正确!
参考: https://www.jianshu.com/p/acfefb0a204f
@Controllerpublic class UploadController extends BaseController {
private static final Log log = LogFactory.getLog(UploadController.class)
private UploadService uploadService
private AuthService authService
/**
* 大文件分成小文件块上传,一次传递一块,最后一块上传成功后,将合并所有已经上传的块,保存到File Server
* 上相应的位置,并返回已经成功上传的文件的详细属性. 当最后一块上传完毕,返回上传成功的信息。此时用getFileList查询该文件,
* 该文件的uploadStatus为2。client请自行处理该状态下文件如何显示。(for UPS Server)
*
*/
@RequestMapping("/core/v1/file/upload")
@ResponseBody
public Object upload(HttpServletResponse response,
@RequestParam(value = "client_id", required = false) String appkey,
@RequestParam(value = "sig", required = false) String appsig,
@RequestParam(value = "token", required = false) String token,
@RequestParam(value = "uuid", required = false) String uuid,
@RequestParam(value = "block", required = false) String blockIndex,
@RequestParam(value = "file", required = false) MultipartFile multipartFile,
@RequestParam Map<String, String>parameters) {
checkEmpty(appkey, BaseException.ERROR_CODE_16002)
checkEmpty(token, BaseException.ERROR_CODE_16007)
checkEmpty(uuid, BaseException.ERROR_CODE_20016)
checkEmpty(blockIndex, BaseException.ERROR_CODE_20006)
checkEmpty(appsig, BaseException.ERROR_CODE_10010)
if (multipartFile == null) {
throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在
}
Long uuidL = parseLong(uuid, BaseException.ERROR_CODE_20016)
Integer blockIndexI = parseInt(blockIndex, BaseException.ERROR_CODE_20006)
Map<String, Object>appMap = getAuthService().validateSigature(parameters)
AccessToken accessToken = CasUtil.checkAccessToken(token, appMap)
Long uid = accessToken.getUid()
String bucketUrl = accessToken.getBucketUrl()
// 从上传目录拷贝文件到工作目录
String fileAbsulutePath = null
try {
fileAbsulutePath = this.copyFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename())
} catch (IOException ioe) {
log.error(ioe.getMessage(), ioe)
throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在
}
File uploadedFile = new File(Global.UPLOAD_TEMP_DIR + fileAbsulutePath)
checkEmptyFile(uploadedFile)// file 非空验证
Object rs = uploadService.upload(uuidL, blockIndexI, uid, uploadedFile, bucketUrl)
setHttpStatusOk(response)
return rs
}
// TODO 查看下这里是否有问题
// 上传文件非空验证
private void checkEmptyFile(File file) {
if (file == null || file.getAbsolutePath() == null) {
throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在
}
}
/**
* 写文件到本地文件夹
*
* @throws IOException
* 返回生成的文件名
*/
private String copyFile(InputStream inputStream, String fileName) {
OutputStream outputStream = null
String tempFileName = null
int pointPosition = fileName.lastIndexOf(".")
if (pointPosition <0) {// myvedio
tempFileName = UUID.randomUUID().toString()// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26
} else {// myvedio.flv
tempFileName = UUID.randomUUID() + fileName.substring(pointPosition)// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26.flv
}
try {
outputStream = new FileOutputStream(Global.UPLOAD_TEMP_DIR + tempFileName)
int readBytes = 0
byte[] buffer = new byte[10000]
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
outputStream.write(buffer, 0, readBytes)
}
return tempFileName
} catch (IOException ioe) {
// log.error(ioe.getMessage(), ioe)
throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在
} finally {
if (outputStream != null) {
try {
outputStream.close()
} catch (IOException e) {
}
}
if (inputStream != null) {
try {
inputStream.close()
} catch (IOException e) {
}
}
}
}
/**
* 测试此服务是否可用
*
* @param response
* @return
* @author zwq7978
*/
@RequestMapping("/core/v1/file/testServer")
@ResponseBody
public Object testServer(HttpServletResponse response) {
setHttpStatusOk(response)
return Global.SUCCESS_RESPONSE
}
public UploadService getUploadService() {
return uploadService
}
public void setUploadService(UploadService uploadService) {
this.uploadService = uploadService
}
public void setAuthService(AuthService authService) {
this.authService = authService
}
public AuthService getAuthService() {
return authService
}
}
usingSystem.xml
string
showtext
=
"null"
System.Xml.XmlTextReader
reader
=
new
XmlTextReader
("Default.xml"))
while
(reader.Read())
{
if
(reader.NodeType
==
XmlNodeType.Element)//判断是否为元素
{
while
(reader.MoveToNextAttribute())
//判断节点有没有属性或有没有下一个属性
{
if
(reader.Value
==
"auther")
//如果属性为auther
则取出此节点中包含的值
{
reader.MoveToContent()//移动到节点包含的内容上
showtext
=
reader.ReadInnerXml()//读取内容
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)