当前位置: 技术问答>java相关
如何从客户端上传图片
来源: 互联网 发布时间:2015-09-04
本文导语: 请教高手。如何从客户端上传图片到servlet。并且我要把图片写入数据,我只会从本地以目录下获得图片并且写入数据库,客户端如何来写,servlet如何获得图片的生成的InputStream,和文件长度(插入数据库时需要的) ...
请教高手。如何从客户端上传图片到servlet。并且我要把图片写入数据,我只会从本地以目录下获得图片并且写入数据库,客户端如何来写,servlet如何获得图片的生成的InputStream,和文件长度(插入数据库时需要的)
|
给你一个servlet,太长了,分两段:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadTest extends HttpServlet {
String rootPath= "c:\uploaded\";
String successMessage = "succeed!";
static final int MAX_SIZE = 1024*1024*10;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("");
out.println("Servlet1");
out.println("");
out.println("");
out.println("");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
ServletOutputStream out=null;
DataInputStream in=null;
FileOutputStream fileOut=null;
try
{
/*set content type of response and get handle to output stream in case we are unable to redirect client*/
response.setContentType("text/plain");
out = response.getOutputStream();
}
catch (IOException e)
{
//print error message to standard out
System.out.println("Error getting output stream.");
System.out.println("Error description: " + e);
return;
}
try
{
String contentType = request.getContentType();
//make sure content type is multipart/form-data
if(contentType != null && contentType.indexOf("multipart/form-data") != -1)
{
//open input stream from client to capture upload file
in = new DataInputStream(request.getInputStream());
//get length of content data
int formDataLength = request.getContentLength();
//allocate a byte array to store content data
byte dataBytes[] = new byte[formDataLength];
//read file into byte array
int bytesRead = 0;
int totalBytesRead = 0;
int sizeCheck = 0;
while (totalBytesRead MAX_SIZE)
{
out.println("Sorry, file is too large to upload.");
return;
}
bytesRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += bytesRead;
}
//create string from byte array for easy manipulation
String file = new String(dataBytes);
System.out.println("file========="+file);
//since byte array is stored in string, release memory
dataBytes = null;
/*get boundary value (boundary is a unique string that
separates content data)*/
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1, contentType.length());
//get Directory web variable from request
String directory="";
if (file.indexOf("name="Directory"") > 0)
{
directory = file.substring(file.indexOf("name="Directory""));
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//get Directory
directory = directory.substring(0,directory.indexOf("n")-1);
/*make sure user didn't select a directory higher in the directory tree*/
if (directory.indexOf("..") > 0)
{
out.println("Security Error: You can't upload " +"to a directory higher in the directory tree.");
return;
}
}
//get SuccessPage web variable from request
String successPage="";
if (file.indexOf("name="SuccessPage"") > 0)
{
successPage = file.substring(file.indexOf("name="SuccessPage""));
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//get success page
successPage = successPage.substring(0,successPage.indexOf("n")-1);
}
//get OverWrite flag web variable from request
String overWrite;
if (file.indexOf("name="OverWrite"") > 0)
{
overWrite = file.substring(file.indexOf("name="OverWrite""));
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
overWrite = overWrite.substring(0,overWrite.indexOf("n")-1);
}
else
{
overWrite = "false";
}
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadTest extends HttpServlet {
String rootPath= "c:\uploaded\";
String successMessage = "succeed!";
static final int MAX_SIZE = 1024*1024*10;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("");
out.println("Servlet1");
out.println("");
out.println("");
out.println("");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
ServletOutputStream out=null;
DataInputStream in=null;
FileOutputStream fileOut=null;
try
{
/*set content type of response and get handle to output stream in case we are unable to redirect client*/
response.setContentType("text/plain");
out = response.getOutputStream();
}
catch (IOException e)
{
//print error message to standard out
System.out.println("Error getting output stream.");
System.out.println("Error description: " + e);
return;
}
try
{
String contentType = request.getContentType();
//make sure content type is multipart/form-data
if(contentType != null && contentType.indexOf("multipart/form-data") != -1)
{
//open input stream from client to capture upload file
in = new DataInputStream(request.getInputStream());
//get length of content data
int formDataLength = request.getContentLength();
//allocate a byte array to store content data
byte dataBytes[] = new byte[formDataLength];
//read file into byte array
int bytesRead = 0;
int totalBytesRead = 0;
int sizeCheck = 0;
while (totalBytesRead MAX_SIZE)
{
out.println("Sorry, file is too large to upload.");
return;
}
bytesRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += bytesRead;
}
//create string from byte array for easy manipulation
String file = new String(dataBytes);
System.out.println("file========="+file);
//since byte array is stored in string, release memory
dataBytes = null;
/*get boundary value (boundary is a unique string that
separates content data)*/
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1, contentType.length());
//get Directory web variable from request
String directory="";
if (file.indexOf("name="Directory"") > 0)
{
directory = file.substring(file.indexOf("name="Directory""));
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//get Directory
directory = directory.substring(0,directory.indexOf("n")-1);
/*make sure user didn't select a directory higher in the directory tree*/
if (directory.indexOf("..") > 0)
{
out.println("Security Error: You can't upload " +"to a directory higher in the directory tree.");
return;
}
}
//get SuccessPage web variable from request
String successPage="";
if (file.indexOf("name="SuccessPage"") > 0)
{
successPage = file.substring(file.indexOf("name="SuccessPage""));
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//get success page
successPage = successPage.substring(0,successPage.indexOf("n")-1);
}
//get OverWrite flag web variable from request
String overWrite;
if (file.indexOf("name="OverWrite"") > 0)
{
overWrite = file.substring(file.indexOf("name="OverWrite""));
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
overWrite = overWrite.substring(0,overWrite.indexOf("n")-1);
}
else
{
overWrite = "false";
}
|
www.jspsmart.com 里面有上传组件啊!
|
有一家公司开发了著名的jspsmartupload上传组件,能同时上传四个文件。
网战:www.javajia.com
下载网址:
http://dboy520.51.net/cgi-bin/javajia/software/jspSmartUpload.zip
网战:www.javajia.com
下载网址:
http://dboy520.51.net/cgi-bin/javajia/software/jspSmartUpload.zip
|
我做过APPLICATION 的,IE 的用
client :
其他的都在服务器端解决,取URL FILENAME 。。。
client :
其他的都在服务器端解决,取URL FILENAME 。。。