Hi
I have a problem in upload file using jsp.At localhost it is working file but at server when client upload a word document it is corrupted .A text file is uploaded successfully.

Please Help

Recommended Answers

All 4 Replies

Would be much helpful if you post your code or at least the crucial part as we are not good in mind reading :idea:

Thanks For reply.AT linux server the code is given below

<%@ page import="java.io.*" %>

<%
try
{
String client=(String)session.getAttribute("clientid");
String contentType = request.getContentType();
FileOutputStream fileOut=null;
String file="";
String SaveFile="";
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;
}

 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("\""));


int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=/"+1);

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;
String appPath = application.getRealPath("/")+"upload files/";  
  
    String dir2=appPath+client+"/";
  File f = new File(dir2);
	boolean ok = f.mkdirs();
	  String destFolder = dir2;  
saveFile= destFolder + saveFile; 
 fileOut = new FileOutputStream(saveFile);


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



}
}
 catch (Exception e){    

   }
%>

YUGH.
Do NOT do any of that in JSP.

Plus consider using Apache Commons File Upload package rather than doing it yourself given the kind of nifty functionality it offers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.