首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

急求JSP上传图片有关问题

2012-03-12 
急求JSP上传图片问题找了好多,就是成功不了!希望曾成功过的把代码发出来,并且把步骤写出来,谢谢了!!![解决

急求JSP上传图片问题
找了好多,就是成功不了!
希望曾成功过的把代码发出来,并且把步骤写出来,谢谢了!!!

[解决办法]

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class UploadHandle {

private ServletInputStream in = null;
private ServletOutputStream out = null;
private HttpServletRequest request = null;
private HttpServletResponse response = null;

private String boundary = " ";
private String strEndFlag = " ";
private boolean bStreamNotEnd = true;
private static String savePath = "./upload/ ";
private int fileSize = 0;

private long maxFileSize = 20000000;


public UploadHandle(HttpServletRequest request, HttpServletResponse
response, String savePath2) {
this.request = request;
this.response = response;
try {
in = request.getInputStream();
out = response.getOutputStream();

if (savePath2 != null && !savePath2.equals( " ")) {
savePath = savePath2;
} else {
String strAppPath = System.getProperty( "user.dir ");
savePath = strAppPath + "/upload/ ";
}
File file = new File(savePath);
file.mkdirs();
file = null;
} catch (Exception e) {
}

}

public void setMaxFileSize(long maxFileSize) {
this.maxFileSize = maxFileSize;
}

public void go() throws Exception {
String strContentType = request.getContentType();
boundary = "-- " +
strContentType.substring(strContentType.indexOf( "boundary= ") + 9);
strEndFlag = boundary + "-- ";

String strParameterName = " ";
String strParameterValue = " ";
String strFileName = " ";
String[] strArrFiles = new String[]{ " ", " ", " ", " "};

String strLine = " ";

Matcher matcher = null;
String strPattern = " ";
strPattern = "Content-Disposition: form-data;
name=\ "([\\p{Print}&&[^\ "]]+)\ "(; filename=\ "(.+)\ ")? ";
bStreamNotEnd = true;
while (bStreamNotEnd && !(strLine = readLine()).equals(strEndFlag)) {
matcher = Pattern.compile(strPattern,
Pattern.CASE_INSENSITIVE).matcher(strLine);
if (matcher.find()) {
strParameterName = matcher.group(1);
strFileName = matcher.group(3);
// System.out.println( "strParameterName: " + strParameterName);
// System.out.println( "strFileName: " + strFileName);

while (!this.readLine().equals( " ")){}

if (strFileName != null) {
strFileName = strFileName.replace( "\\ ", "/ ");
if(strFileName.lastIndexOf( "/ ") > -1) {
strFileName = strFileName.substring(strFileName.lastIndexOf( "/ ") + 1);
}
String str2 = " ";
if(strFileName.indexOf( ". ") > -1) {
str2 = strFileName.substring(strFileName.lastIndexOf( ". "));
}

strFileName = (new DateUtil()).getDateTime().replace( ": ", "_ ").replace( "
", "_ ") + "_ " + ((int) (Math.random() * 10000000)) + " " + str2; //(new
DateUtil()).getDateTime().replace( ": ", " ' ").replace( " ", "_ ") + "_ " +



String filePath = savePath + strFileName;
saveFile(filePath);

if (fileSize > 0) {
String strTemp = (String)request.getAttribute(strParameterName);
if (strTemp != null && !strTemp.equals( " ")) {
request.setAttribute(strParameterName, strTemp + ";/ " + filePath);
String strTemp2 = (String)request.getAttribute( "countSum ");
request.setAttribute( "fileSize ", strTemp2 + ";/ " + fileSize);
} else {
request.setAttribute(strParameterName, filePath);
request.setAttribute( "fileSize ", fileSize);
}
}
} else {
strLine = readLine(); // 读取参数值
strParameterValue = strLine;
// System.out.println( "strParameterValue: " + strParameterValue);
String strTemp = (String)request.getAttribute(strParameterName);
if (strTemp != null && !strTemp.equals( " ")) {
request.setAttribute(strParameterName, strTemp + ";/ " + strParameterValue);
} else {
request.setAttribute(strParameterName, strParameterValue);
}
}
}
}
}

private String readLine() throws Exception {
String theResult = " ";

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int iByte = 0;
while ((iByte = in.read()) != -1) {
baos.write(iByte);
if (iByte == 10) {
theResult = baos.toString().replaceAll( "[\r\n] ", " ");
break;
}
}

// System.out.println(theResult);
return theResult;
}

private Object[] getLineDataWithRtLf() throws Exception {
// buffer, readCount, lnFlag
Object[] theResult = null;
int bufferSize = 128;
byte[] buffer = new byte[bufferSize];
int readCount = 0;
boolean lnFlag = false;

int iByte = 0;
int index = 0;
while ((iByte = in.read()) != -1) {
// System.out.print((char)iByte);
buffer[index] = (byte)iByte;
if (iByte == 10) {
readCount = index + 1;
lnFlag = true;
break;
}
if (index == bufferSize - 1) {
readCount = bufferSize;
break;
}
index++;
}

theResult = new Object[]{buffer, String.valueOf(readCount),
String.valueOf(lnFlag)};
return theResult;
}

private void saveFile(String filePath) throws Exception {
fileSize = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(filePath);
bos = new BufferedOutputStream(fos, 4096);
} catch(Exception e) {
e.printStackTrace();
}

Object[] objArrTemp = null;
byte[] buffer_1 = null;
byte[] buffer_2 = null;
int readCount_1 = 0;
int readCount_2 = 0;
boolean lnFlag = false;

objArrTemp = this.getLineDataWithRtLf();
buffer_1 = (byte[])objArrTemp[0];
readCount_1 = Integer.parseInt((String)objArrTemp[1]);

while (true) {
objArrTemp = this.getLineDataWithRtLf();
buffer_2 = (byte[])objArrTemp[0];
readCount_2 = Integer.parseInt((String)objArrTemp[1]);
lnFlag = Boolean.parseBoolean((String)objArrTemp[2]);

if (lnFlag == true) {
String strTemp = new String(buffer_2, 0, readCount_2);

if (strTemp.indexOf(boundary) > -1) {
// System.out.print(new String(buffer_1, 0, readCount_1 - 2));
try {
fileSize += readCount_1;
if (fileSize > maxFileSize) {
bos.close();
throw new Exception( "文件太大。fileSize > " + maxFileSize);
}
bos.write(buffer_1, 0, readCount_1 - 2);
}catch(Exception e){throw e;}
if (strTemp.indexOf(strEndFlag) > -1) {
this.bStreamNotEnd = false;
}
break;
}
}
// System.out.print(new String(buffer_1, 0, readCount_1));



fileSize += readCount_1;
if (fileSize > maxFileSize) {
bos.close();
throw new Exception( "文件太大。fileSize > " + maxFileSize);
}
bos.write(buffer_1, 0, readCount_1);

buffer_1 = buffer_2;
readCount_1 = readCount_2;
}

try {
bos.flush();
fos.close();
} catch(Exception e) {
e.printStackTrace();
}
}

}

[解决办法]
<%@ page contentType= "text/html;charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " "%>
<!DOCTYPE HTML PUBLIC "_//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">

<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">

<title> 将图象以二进制数据格式存储到数据库中 </title>
<style type= "text/css ">

<!--
.style1 {
color: #FF0000;
font-size: 24px;
}
-->

</style>
</head>
<body>
<div align= "center ">
<span class= "style1 "> 将图象以二进制数据格式存储到数据库中 </span> <BR>

</div>
<hr>
<BR>
<body>
<form name= "form1 " method= "post " action= "testimage.jsp ">
<p align= "center "> 请输入图片id:&nbsp;&nbsp;&nbsp;
<input type= "text " name= "id ">
</P>
<p align= "center "> 请输入图片的URL:
<input type= "text " name= "image ">
</P>
<p align= "center ">
<input type= "submit " name= "Submit " value= "提交 ">
</P>
</form>
</body>
</html>



[解决办法]
我来给你个例子吧,但需要jspmart组件的,如果没有我传你.

<HTML> <BODY>
<FORM ENCTYPE= "MULTIPART/FORM-DATA " METHOD= "POST " ACTION= "/test/servletUpload ">
文件名称:
  <INPUT TYPE= "file " NAME= "ulfile ">
  <BR> 存储到服务器上的路径:
  <INPUT TYPE= "TEXT " name= "PATH "> <BR>
  <INPUT TYPE= "submit " value= "上传 ">
  <INPUT TYPE= "reset " value= "清除 ">
</FORM>
</BODY> </HTML>
下面是servletUpload(这个是servlet)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;

public class servletUpload extends HttpServlet {

private ServletConfig config;
final public void init(ServletConfig config)throws ServletException{
this.config=config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//response.setContentType( "text/html ");
PrintWriter out = response.getWriter();
out.println( "The method of the HTML form must be POST ");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
PrintWriter out=response.getWriter();
int count=1;


SmartUpload mySmartUpload=new SmartUpload();
try{
mySmartUpload.initialize(config,request,response);
mySmartUpload.upload();
mySmartUpload.save(mySmartUpload.getRequest().getParameter( "PATH "));
out.println(count+ "file uploaded. ");
}catch(Exception e){
out.println( "Unable to upload the file. <br> ");
out.println( "Error: "+e.toString());
}
}

}

热点排行
Bad Request.