JSP上传图片问题

攻城狮·正 2010-09-26 02:20:45
我用自制JSP上传组件,上传某些图片的时候出现文件损坏,不能打开的情况,有的图片只能显示一半。不像是图片大小问题。1张110K的图片出来了,30.3K的却没出来,难道是流分割有问题,请高手帮我看一下。

package com.web.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

/**
* 解析ServletInputStream,实现文件上传
* @version 2010/09/23
*/
public class SubmitStreamAnalysis {

private HashMap<String, String> params; // 参数
private HashMap<String, FileStream> file; // 文件
private ServletInputStream sis; // 解析流
private String cutStr; // 分割字符串
private int maxSize; // 最大文件字节数
private String encoding; // 字符编码

public SubmitStreamAnalysis(HttpServletRequest request, int maxSize) {

try {
this.sis = request.getInputStream();
this.maxSize = maxSize;
this.encoding = "gb2312";
params = new HashMap<String, String>();
file = new HashMap<String, FileStream>();
init();
} catch (IOException e) {
e.printStackTrace();
}
}

public SubmitStreamAnalysis(HttpServletRequest request, int maxSize, String encoding) {

try {
this.sis = request.getInputStream();
this.maxSize = maxSize;
this.encoding = encoding;
params = new HashMap<String, String>();
file = new HashMap<String, FileStream>();
init();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 初始化
* @throws IOException
*/
private void init() throws IOException {

byte[] b = new byte[maxSize];

int size = sis.readLine(b, 0, b.length); // 读取第一行
String str = null; // 临时存储一行字符串
cutStr = new String(b, 0, 29); // 分割线:----------

while (size != -1) {
str = new String(b, 0, size); // 得到一行的字符串
// 排除分割线和空行
if (str.indexOf(cutStr) != -1 && str.trim().equals("")) {
continue;
}
// 是否属于参数
if (isParam(str)) {
// 键
String temp = str.substring(str.indexOf("name") + 6);
String key = temp.substring(0, temp.indexOf("\""));
// 是否是文件参数
if (isFile(str)) {
// 存入文件
String source = null;
String fileName = null;
String suffix = null;
while(size != -1) {
str = new String(b, 0, size);
if (str.indexOf(cutStr) != -1) {
break;
}
if (str.trim().equals("")) {
continue;
}

if (str.indexOf("filename") != -1 && str.indexOf(".") != -1) {
source = str.substring(str.indexOf("filename=\"") + 1, str.lastIndexOf("\""));
fileName = str.substring(str.lastIndexOf("\\") + 1, str.lastIndexOf("."));
suffix = str.substring(str.lastIndexOf("."), str.lastIndexOf("\""));
}
if (str.indexOf("Content-Type:") != -1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
size = sis.readLine(b, 0, b.length);
while (size != -1) {
size = sis.readLine(b, 0, b.length);
str = new String(b, 0, size);
if (str.indexOf(cutStr) != -1) {
break;
}
if (str.trim().equals("")) {
continue;
}
baos.write(b, 0, size);
}
FileStream fs = new FileStream();
fs.setSource(source);
fs.setFileName(fileName);
fs.setSuffix(suffix);
fs.setData(baos.toByteArray());
file.put(key, fs);
baos.close();
}
size = sis.readLine(b, 0, b.length);
}
} else {
// 存入参数
StringBuffer value = new StringBuffer();
while (size != -1) {
size = sis.readLine(b, 0, b.length);
str = new String(b, 0, size, encoding);
if (str.indexOf(cutStr) != -1) {
break;
}
if (str.trim().equals("")) {
continue;
}
value.append(str);
}
params.put(key, value.toString());
}
}
size = sis.readLine(b, 0, b.length);
}
}

// 判断是否是文件
private Boolean isFile(String str) {

boolean flag = false;
if (str.indexOf("Content-Disposition:") != -1 && str.indexOf("name") != -1
&& str.indexOf("filename") != -1) {
flag = true;
}
return flag;
}

// 判断是否是参数
private Boolean isParam(String str) {

boolean flag = false;
if (str.indexOf("Content-Disposition:") != -1 && str.indexOf("name") != -1) {
flag = true;
}
return flag;
}

/**
* 获得属性值
* @param name
* @return
*/
public String getParameter(String name) {

if (params.containsKey(name)) {
return params.get(name);
}
return null;
}

/**
* 获得文件对象
* @param name
* @return
*/
public FileStream getFile(String name) {

if (file.containsKey(name)) {
return file.get(name);
}
return null;
}
}


package com.web.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 文件上传流
* @author
* @version 2010/09/25
*/
public class FileStream {

private String source = null; // 源
private String fileName = null; // 文件名
private String suffix = null; // 后缀名
private String path = null; // 保存路径
private byte[] data = null; // 文件流

/**
* 写入磁盘
* @param path
*/
public void toDisk(String path) {
if (path == null) {
System.out.println("路径不能为空");
return;
}
File f = new File(path + "/" + fileName + suffix);

try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}

public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
craaot_0319 2010-09-29
  • 打赏
  • 举报
回复
可以用插件~~
LeeJah163 2010-09-26
  • 打赏
  • 举报
回复
我的妈呀,就上传一个图片,搞的这么复杂~~~
而且也请你把代码放到
//Java代码
里面去,你那代码那么多,那么乱,人家懒得看的...

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧