Java OSS Image Upload Controller | Generated by AI

Home 2025.10

How the Code Works

This is a Java Spring Boot controller (FileController) designed to handle file uploads via an HTTP POST request. It integrates with Alibaba Cloud Object Storage Service (OSS) to store uploaded files in the cloud. The code focuses on image uploads (restricting to JPEG/PNG formats) and provides a simple, unauthenticated endpoint. It uses temporary local file storage during the process before deleting it.

Key Components and Flow:

  1. Class Structure:
    • It’s a @RestController mapped to the base path "file", extending BaseController (likely for shared logic).
    • The main method is upload(), mapped to "/file/upload".
    • Annotations:
      • @RequestMapping: Defines the endpoint and allowed HTTP method (POST).
      • @ResponseBody: Ensures the response is JSON (via LQResponse).
      • @NoAuth: Indicates no authentication is required for this endpoint (custom AOP annotation).
  2. Dependencies:
    • Spring Framework (e.g., @RestController, @RequestMapping, @RequestParam, MultipartFile for file handling).
    • Aliyun OSS SDK (e.g., OSSClient for interactions with OSS).
    • Apache Commons Lang (e.g., RandomStringUtils for generating random keys, StringUtils for string manipulation).
    • Custom classes like LQException, LQError, and LQResponse (likely part of your app’s error handling and response utilities).
  3. Step-by-Step Breakdown of the upload() Method:
    • Input Validation:
      • Receives a MultipartFile (the uploaded file).
      • Determines the content type (MIME type) using URLConnection.guessContentTypeFromStream(). This checks if the file is truly an image file based on its bytes.
      • Allows only specific types: "image/jpeg", "image/jpg", or "image/png". If not, throws a LQException with error code UNSUPPORTED_IMAGE_FILE.
      • Extracts the file extension (e.g., .jpg) from the content type.
    • File Preparation:
      • Creates a temporary local File object using the original filename.
      • Writes the file’s bytes to the local disk using FileOutputStream. This step is necessary because the OSS SDK’s putObject requires a File or InputStream.
    • OSS Upload:
      • Initializes an OSSClient with:
        • Endpoint: https://oss-cn-qingdao.aliyuncs.com (Qingdao region in China).
        • Access Key ID: "LTAIuXm7.. (hardcoded—note: In production, this should be loaded securely from environment variables or a config file to avoid exposing credentials).
        • Secret Access Key: "GP8FRF..." (also hardcoded—same security note).
        • Bucket: Empty string ("")—this is likely a placeholder and must be set to a valid OSS bucket name (e.g., "my-bucket").
      • Generates a unique object key: A random 6-character alphanumeric string + the file extension (e.g., a3bS9k.jpg).
      • Calls ossClient.putObject() with a PutObjectRequest pointing to the bucket, key, and local file. This uploads the file to OSS.
      • Catches OSSException (OSS-side errors) or ClientException (client/network errors) and throws a custom LQException with error code FILE_UPLOAD_FAIL.
    • Cleanup and Response:
      • Deletes the temporary local file with newFile.delete() to avoid disk clutter.
      • Returns an LQResponse.success() with the public URL of the uploaded file: FILE_HOST + "/" + key.
        • FILE_HOST is another empty string placeholder—set it to your OSS bucket’s domain (e.g., "https://my-bucket.oss-cn-qingdao.aliyuncs.com").
    • Error Handling: Uses custom exceptions (LQException) for validation and upload failures, ensuring consistent app-wide error responses.

Security Notes:

How to Use the Aliyun OSS SDK Imports

The listed imports are for the Alibaba Cloud OSS Java SDK (typically added via Maven/Gradle as com.aliyun.oss:aliyun-sdk-oss). They provide classes for interacting with OSS. Below is how each is used in context, with examples.

  1. import com.aliyun.oss.OSSClient;:
    • The main client class for OSS operations (now deprecated in favor of OSSClientBuilder, but still functional in older codebases).
    • Usage: Create an instance to connect to OSS.
      OSSClient ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
      // Then use methods like putObject(), getObject(), deleteObject().
      
    • Why Here: Used to authenticate and upload the file to the specified bucket.
  2. import com.aliyun.oss.ClientException;:
    • Thrown for client-side issues (e.g., network failures, invalid credentials).
    • Usage: Catch it to handle errors.
      try {
          // OSS operation
      } catch (ClientException e) {
          // Handle client errors (e.g., retry or log)
      }
      
    • Why Here: Caught in the upload method for resilient error handling.
  3. import com.aliyun.oss.OSSException;:
    • Thrown for OSS service-side errors (e.g., bucket not found, permission denied).
    • Usage: Similar to ClientException, but service-specific.
      try {
          // OSS operation
      } catch (OSSException e) {
          // Log e.getErrorCode() and e.getErrorMessage()
      }
      
    • Why Here: Caught to provide user-friendly failure messages via LQException.
  4. import com.aliyun.oss.model.PutObjectRequest;:
    • A model class for building upload requests (includes bucket, key, file/input stream, metadata).
    • Usage:
      PutObjectRequest request = new PutObjectRequest(BUCKET, KEY, FILE);
      // Optional: Add metadata, e.g., request.setObjectMetadata(metadata);
      ossClient.putObject(request);
      
    • Why Here: Used to specify the upload details (bucket, key, local file).
  5. import com.aliyun.oss.model.PutObjectResult;:
    • Returned by putObject(); contains upload metadata (e.g., ETag for integrity checks).
    • Usage:
      PutObjectResult result = ossClient.putObject(request);
      String etag = result.getETag();  // For verification
      
    • Why Here: Assigned but not deeply used in the code (could be logged for debugging).

Setup and Integration Tips:

If you provide more details (e.g., specific errors or customizations), I can refine this further!


Back

x-ai/grok-code-fast-1

Donate