Estou com o problema serio
Alguem pode me enviar um codigo java para fazer uploded.
Exemplo:
Preciso de um jsp que pede para inserir um arquivos e java pega esse arquivo e envia para um outro servidor.
eu tenho esse codigo :
<%!
void deleteFiles( String uploadLocation ){
if( uploadLocation.indexOf("upload_tmp")==-1 ) return;
try{
File file = new File(uploadLocation);
File files[] = file.listFiles();
for( int i=0 ; i<files.length ; i++ ){
File tmpFile = files[i];
if( files[i].isDirectory() ) continue;
try{
long lastModified = tmpFile.lastModified();
Calendar cal = Calendar.getInstance();
cal.add( Calendar.HOUR , -6 );
long aDayAgo = cal.getTime().getTime();
if( lastModified<=aDayAgo ){
// System.out.println( tmpFile.getAbsolutePath() );
tmpFile.delete();
}
}catch( Exception e ){
e.printStackTrace();
}
}
}catch( Exception e ){
e.printStackTrace();
}
}
%>
<%
String uploadLocation = "/acube/sagspn/upload_tmp/service".concat("/");
deleteFiles(uploadLocation);
//image pass
String imagePath = Constants.IMAGE_PATH.concat("/").concat( userVO.getLocale() );
//multi language
MSGBuffer mb_conts = getContentsMsgBuffer( code_com_user_language , "service_tracking" , "SVC_Upload" );
%>
<html>
<head>
<title></title>
<link href="<%=Constants.CSS_PATH%>/body.css" rel=“stylesheet” type=“text/css”>
<script language=“JavaScript” type=“text/JavaScript”>
function verify(){
var objForm = document.all.SVC_FILE_UPLOAD;
var fileName = trim(objForm.svc_files.value);
if( fileName=="" ){
alert("<%=mb_alert.getMessage("WF_SELECTFILE")%>");
return;
}
if( fileName.indexOf(".")==-1 ){
alert("<%=mb_alert.getMessage("FILEEXTENSION")%>");
return;
}
var fileExt = fileName.substring( fileName.lastIndexOf(".") );
if( fileExt!=".txt" ){
alert("<%=mb_alert.getMessage("FILEEXTENSION")%>");
return;
}
showDataprocessBar();
objForm.submit();
}
function viewFileInfo(){
var objForm = document.all.SVC_FILE_UPLOAD;
objForm.txt_svc_files.value = objForm.svc_files.value;
}
</script>
</head>
<body leftmargin=“0” topmargin=“0” marginwidth=“0” marginheight=“0”><a></a>
<table width=“100%” height=“100%” border=“0” cellpadding=“0” cellspacing=“0”>
<tr>
<%@include file="/gspn/common/GSPN_LeftMenu.jsp" %>
<!-- Main Block S -->
<td valign=“top” class=“Rbg”>
<!-- Main Title Block S -->
<%@include file="/gspn/common/GSPN_MainNavigation.jsp" %>
<table width=“100%” border=“0” cellspacing=“0” cellpadding=“0”>
<tr>
<td class=“Tbg_left”>
<table border=“0” cellspacing=“0” cellpadding=“0”>
<tr>
<td class=“Ttxt_main”><%=mb_conts.getMessage(“SVC_FILE_UPLOAD”)%></td>
</tr>
</table>
</td>
<td class=“Tbg_mid”> </td>
<td class=“Tbg_right”> </td>
</tr>
</table>
<!-- Main Title Block E -->
<table width=“100%” border=“0” cellpadding=“0” cellspacing=“0”>
<!-- form start -->
<form name="SVC_FILE_UPLOAD" action="VerifySVCRequest.jsp" method="post" enctype="multipart/form-data" >
<tr>
<td class="STdeco_Ltop"></td>
<td colspan="2" class="STdeco_top"><img ></td>
<td class="STdeco_Rtop"></td>
</tr>
<tr>
<td class="STdeco_left"><img ></td>
<td class="ST_con">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" class="ST_head"><%=mb_conts.getMessage("SVC_DATA_LOCATION")%></td>
<td colspan="2" class="ST_data">
<input name="txt_svc_files" type="text" size="56" class="Fdefault" readonly >
<div name="divFile" id="divFile" style="cursor:hand; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(opacity = 0); width:80; height:21; overflow: hidden; align: right;" NOWRAP align="right">
<input type="file" name="svc_files" style="cursor:hand; border: 0 0 0 0; width:44; height:21;" onChange="viewFileInfo();">
</div>
<img >
<a ><img ></a></td>
</tr>
</table>
</td>
<td width="100" align="center" valign="bottom" class="ST_con"> </td>
<td class="STdeco_right"><img ></td>
</tr>
<tr>
<td class="STdeco_Rtop"></td>
<td colspan="2" class="STdeco_btm"><img ></td>
<td class="STdeco_Ltop"></td>
</tr>
</form>
<!-- form end -->
</table>
<!-- Main Block E -->
</td>
</tr>
<tr>
<td valign=“bottom”>
<table width=“100%” border=“0” cellpadding=“0” cellspacing=“0”>
<tr>
<td class=“Top”><a><img></a></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<%@include file="/gspn/common/GSPN_Footer.jsp" %>
e esse acessa o servidor
<%–
/*
-
File name : WBUploadFile.jsp
*/
–%>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“multipart/form-data; charset=utf-8” />
<title>Untitled Document</title>
</head>
<body>
<%@page import=“org.apache.commons.fileupload.*” %>
<%@page import=“java.util.*” %>
<%@page import=“java.text.SimpleDateFormat” %>
<%@page import=“gspn.sys.user.vo.SYSUserVO”%>
<%@page import=“gspn.common.util.*” %>
<%@page import=“org.apache.commons.net.ftp.FTPReply” %>
<%@page import=“org.apache.commons.net.ftp.FTPClient” %>
<%
String billno = request.getParameter(“billno”);
String data = (new java.text.SimpleDateFormat(“yyyyMMdd”).format(new java.util.Date()));
String result = null;
List str = new ArrayList();
String nome_file = null;
StringTokenizer st = null;
String args[] = new String[1];
SYSUserVO userVo = SessionUtil.getUser(request);
if (){FileUpload.isMultipartContent(request)
try{
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeMax(50*1024*1024); //50Mb
upload.setRepositoryPath("/home/sagspn/images");
// Parse the request
List items = upload.parseRequest(request);
File file = null;
Iterator it = items.iterator();
while (it.hasNext())
{
FileItem item = (FileItem) it.next();
if (!item.isFormField())
{
nome_file = item.getName();
if (!nome_file.equals(""))
{
st = new StringTokenizer(nome_file);
while (st.hasMoreTokens())
{
result = st.nextToken("\\");
}
//str.add(result);
file = new File("/home/sagspn/images/"+userVo.getB2bId()+ "_" + billno + "_" + data + "_"+result);
item.write(file);
}
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
//ftp
/*for (Iterator iterator = ; str.iterator()iterator.hasNext(); )
{*/
//String name = (String) iterator.next();
String name = result;
System.out.println("name: "+name);
args[0] = "/home/sagspn/images/"+userVo.getB2bId()+ "_" + billno + "_" + data +"_"+name;
String nomeArquivo = null;
FTPClient ftp = new FTPClient();
try {
ftp.connect("0.0.0.0");
// verifica se conectou com sucesso!
if (FTPReply.isPositiveCompletion(ftp.getReplyCode()))
{
ftp.login("e", "2");
System.out.println("Conectou !");
}
else
{
// erro ao se conectar
ftp.disconnect();
System.out.println("Conexao recusada");
//System.exit(1);
}
ftp.changeWorkingDirectory("../../../usr");
// para cada arquivo informado...
for (int i = 0; i < args.length; i++)
{
// abre um stream com o arquivo a ser enviado
System.out.println(args[i]);
InputStream is = new FileInputStream(args[i]);
// pega apenas o nome do arquivo
int idx = args[i].lastIndexOf(File.separator);
if (idx < 0)
idx = 0;
else
idx++;
nomeArquivo = args[i].substring(idx, args[i].length());
// ajusta o tipo do arquivo a ser enviado
if (args[i].endsWith(".txt"))
{
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
else if (args[i].endsWith(".jpg"))
{
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
}
else
{
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
System.out.println("Enviando arquivo " + nomeArquivo + "...");
// faz o envio do arquivo
ftp.storeFile(nomeArquivo, is);
System.out.println("Arquivo " + nomeArquivo
+ " enviado com sucesso!");
}
ftp.disconnect();
System.out.println("Fim.");
} catch (Exception e)
{
System.out.println("Ocorreu um erro: " + e);
//System.exit(1);
}
//}
}
%>
<SCRIPT language=“javascript”>
history.back(1)
alert(‘Arquivo Carregado’);
window.location.reload(false)
</SCRIPT>
</body>
</html>
O que eu quero e que recebe o arquivo e envia por ftp