解决方案 »

  1.   

    文件很大吗?如果大,建议分块上传。Java 文件分块上传客户端源代码Java 文件分块上传服务器端源代码
      

  2.   

    刚好,这里也有个服务器端处理上传的源代码,是 spring mvc 框架下的:
    @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 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;
    }}
      

  3.   


    需求是只要有文件提交,过了验证之后,就能马上响应写到本地,分块的话,客户端的实现不是java.怎么搞....  o(︶︿︶)o 唉
      

  4.   


    带宽小的话,它也可以一点一点写到文件里面去,现在的情况是它要将整个文件缓存到服务器(temp文件),代码才会继续往下跑,如果中途缓存了90%大小的文件,网络断开了,因为没有写到服务器目标文件上,它又白上传了.
    我的需求是:客户端上传,服务器不缓存到本地(不生成临时文件),直接写到文件中..  
    有做过类似的例子么?