CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 I have only correct solution...

Print topic Send  topic

Author Message
Szymon Splawski
Posted: 05/20/2005, 9:27 AM

//--------------------------------------------------------------------------------------------------------
JSP FILE:
//-------------
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();

//out.println("Content type is :: " +contentType);

if((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;


while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}


String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

//out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());

//out.println(boundary);

int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;


int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

//out.println(dataBytes);

FileOutputStream fileOut = new FileOutputStream(saveFile);

//-----------------------------------------------------------------------------------
%>
//you need create useBean to acces base64 methods class
<jsp:useBean id="workFlow" scope="session" class="logic.base64" />
<%
//-----------------------------------------------------------------------------------
byte dataBytesFrom[] = workFlow.encode(dataBytes);
String s=new String(dataBytesFrom);

//Here save s string into databse
//and get this string from there

byte dataBytesFrom2[] = workFlow.getBinaryBytes(s);
dataBytesFrom2 = workFlow.decode(dataBytesFrom2);

//-----------------------------------------------------------------------------------

fileOut.write(dataBytesFrom2, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

out.println("File saved as " +saveFile);
}
%>

<form method="post" ACTION="upload.jsp" name="upform" ENCTYPE="multipart/form-data">
<p>
<input type="file" name="uploadfile">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
<input type="hidden" name="action" value="upload">
</p>
</form>
//--------------------------------------------------------------------------------------------------------
base64.java file
//------------------------
package logic;

public class base64 {
public static String encode(String data) {
return(getString(encode(getBinaryBytes(data))));
}

public static byte[] encode(byte[] data) {
int c;
int len = data.length;
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);

for (int i = 0; i < len; ++i) {
c = (data >> 2) & 0x3f;
ret.append(cvt.charAt(c));
c = (data << 4) & 0x3f;

if (++i < len) {
c |= (data >> 4) & 0x0f;
}
ret.append(cvt.charAt(c));

if (i < len) {
c = (data << 2) & 0x3f;
if (++i < len) {
c |= (data >> 6) & 0x03;
}

ret.append(cvt.charAt(c));
}
else {
++i;
ret.append((char) fillchar);
}

if (i < len) {
c = data & 0x3f;
ret.append(cvt.charAt(c));
}
else {
ret.append((char) fillchar);
}
}
return(getBinaryBytes(ret.toString()));
}

public static String decode(String data) {
return(getString(decode(getBinaryBytes(data))));
}

public static byte[] decode(byte[] data)
{
int c;
int c1;
int len = data.length;
StringBuffer ret = new StringBuffer((len * 3) / 4);

for (int i = 0; i < len; ++i) {
c = cvt.indexOf(data);
++i;
c1 = cvt.indexOf(data);
c = ((c << 2) | ((c1 >> 4) & 0x3));
ret.append((char) c);

if (++i < len) {
c = data;

if (fillchar == c) break;

c = cvt.indexOf((char) c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append((char) c1);
}

if (++i < len) {
c1 = data;

if (fillchar == c1) break;

c1 = cvt.indexOf((char) c1);
c = ((c << 6) & 0xc0) | c1;
ret.append((char) c);
}
}
return(getBinaryBytes(ret.toString()));
}

public static String getString(byte[] arr) {
StringBuffer buf = new StringBuffer();

for (int i = 0; i < arr.length; ++i) {
buf.append((char) arr);
}
return(buf.toString());
}

public static byte[] getBinaryBytes(String str) {
byte[] b = new byte[str.length()];

for (int i = 0; i < b.length; ++i) {
b = (byte) str.charAt(i);
}
return(b);
}

public static final int fillchar = '=';

// 00000000001111111111222222 //
// 01234567890123456789012345 //
public static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// 22223333333333444444444455 //
// 67890123456789012345678901 //
+ "abcdefghijklmnopqrstuvwxyz"

// 555555556666 //
// 234567890123 //
+ "0123456789+/";
}
//--------------------------------------------------------------------------------------------------------

best regard Szymon

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.