「java上传文件流」java 文件上传
本篇文章给大家谈谈java上传文件流,以及java 文件上传对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java中怎样上传文件
Java代码实现文件上传
FormFile file=manform.getFile();
String newfileName = null;
String newpathname=null;
String fileAddre="/numUp";
try {
InputStream stream = file.getInputStream();// 把文件读入
String filePath = request.getRealPath(fileAddre);//取系统当前路径
File file1 = new File(filePath);//添加了自动创建目录的功能
((File) file1).mkdir();
newfileName = System.currentTimeMillis()
+ file.getFileName().substring(
file.getFileName().lastIndexOf('.'));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/"
+ newfileName);
newpathname=filePath+"/"+newfileName;
System.out.println(newpathname);
// 建立一个上传文件的输出流
System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
如何实现java 流式文件上传
@Controller
public 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 MapString, 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);
MapString, 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;
}
}
java如何实现文件上传
public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize - receiveLen bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return receiveLen;
}
这个方法从InputStream中读取内容,写到OutputStream中。
那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。从Blob能获取一个输出流。
java上传文件流的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 文件上传、java上传文件流的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。