java servlet接收图片上传,【分享】uploader上传,java servlet 接收数据代码

ServletFileUpload servletFileUpload = new ServletFileUpload();

FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);

byte[] imageDatas = null;

String imageName = null;

String imgFormatName = null;

if (fileItemIterator.hasNext())

{

FileItemStream fileStream = fileItemIterator.next();

imageName = fileStream.getName();

if (imageName == null || imageName.trim().length() == 0)

{

throw new Exception("文件名称为空");

}

imageName = imageName.toLowerCase();

if (imageName.endsWith("jpg") || imageName.endsWith("jpeg"))

{

imgFormatName = "jpg";

}

else if (imageName.endsWith("png"))

{

imgFormatName = "png";

}

else if (imageName.endsWith("gif"))

{

imgFormatName = "gif";

}

else

{

throw new Exception("只支持:jpg、png、gif三种图片");

}

ByteArrayOutputStream baos = new ByteArrayOutputStream();

InputStream inputStream = null;

try {

inputStream = fileStream.openStream();

BufferedImage bufferedImage = ImageIO.read(inputStream);

int width = bufferedImage.getWidth();

int height = bufferedImage.getHeight();

BufferedImage needSavedImage = bufferedImage;

if (width > 600) {

needSavedImage = org.imgscalr.Scalr.resize(bufferedImage, Method.SPEED, 600, Float.valueOf(height * (600.0f/width)).intValue(), org.imgscalr.Scalr.OP_BRIGHTER);

}

else if (height > 600) {

needSavedImage = org.imgscalr.Scalr.resize(bufferedImage, Method.SPEED, Float.valueOf(width * (600.0f/height)).intValue(), 600, org.imgscalr.Scalr.OP_BRIGHTER);

}

ImageIO.write(needSavedImage, imgFormatName, baos);

imageDatas = baos.toByteArray();

}

catch(Exception e)

{

Logger.error(e.getMessage(), e);

}

finally

{

if (inputStream != null)

{

try {

inputStream.close();

}

catch(Exception e)

{

Logger.error(e.getMessage(), e);

}

}

}

}

这是一个简单的例子:

1) 图片格式只是通过扩展名来做了个简单地过滤

2) 图片的压缩,逻辑简单,够用了,更复杂的逻辑,自个儿补充就行

3)用到了两个库:apache的common upload 以及图片工具类库 org.imgscalr.Scalr (github上有源码,自个儿搜)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,请参考以下示例代码来实现图片上传的功能: ```java import java.io.File; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.HttpClientBuilder; public class ImageUploader { public static void main(String[] args) { String url = "http://example.com/upload"; // 替换为实际的上传地址 String filePath = "path/to/image.jpg"; // 替换为实际的图片路径 HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); try { File imageFile = new File(filePath); FileBody fileBody = new FileBody(imageFile, ContentType.DEFAULT_BINARY); HttpEntity entity = MultipartEntityBuilder.create() .addPart("file", fileBody) .build(); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); // 处理服务器响应,根据实际需求进行操作 } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请确保在运行代码之前,替换 `url` 和 `filePath` 变量的值为实际的上传地址和图片路径。这段代码使用 Apache HttpClient 库来执行 POST 请求,将图片文件作为 `file` 参数进行上传。你可以根据实际情况对服务器响应进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值